<?php

namespace Index\Controller;

use Think\Controller;
use Index\ViewModel;
use Index\Service;

/**
 * 说明：用户管理控制器
 * 作者：王楠
 * 创建日期：2017-09-15
 */
class UserController extends CommonController {

    /**
     * 全部用户列表
     */
    public function userlist() {
	$sex_enum = getenumbykey('sex');
	$sex_arr = getarrbyenum($sex_enum);
	$id_state_enum = getenumbykey('useridentity_state');
	$id_state_arr = getarrbyenum($id_state_enum);
	$yn_enum = getenumbykey('yn');
	$recom_arr = getarrbyenum($yn_enum);
	$active_arr = getarrbyenum($yn_enum);
	$hot_arr = getarrbyenum($yn_enum);
	$agent_arr = getarrbyenum($yn_enum);
	$flag_enum = getenumbykey('flag');
	$flag_arr = getarrbyenum($flag_enum);
	$state_enum = getenumbykey('userlogin_state');
	$state_arr = getarrbyenum($state_enum);
	$sex = trim(I('ddlsex'));
	$idstate = trim(I('ddlidstate'));
	$recom = trim(I('ddlrecom'));
	$active = trim(I('ddlactive'));
	$hot = trim(I('ddlhot'));
	$flag = trim(I('ddlflag'));
	$agent = trim(I('ddlagent'));
	$appcode = trim(I('ddlapp'));
	$state = trim(I('ddlstate'));
	$begintime = trim(I('txtbegintime'));
	$endtime = trim(I('txtendtime'));
	$keyword = trim(I('txtkeyword'));
	$searchtype = trim(I('hidsearchtype'));
	switch ($searchtype) {
	    case '1':

		break;
	    case '2':
		if ($idstate == '3') {
		    
		} else {
		    $flag = '0';
		}
		break;
	    case '3':
		if (!empty($flag)) {
		    $idstate = '3';
		}
		break;
	    case '4':

		break;
	    default:
		break;
	}
	$app_code = $this->app_code;
	$login_type = $this->login_type;
	$admin_uid = $this->admin_uid;
	$map = array('_complex' => '1=1');

	if (!empty($app_code)) {
	    $map['app_flag'] = $app_code;
	} else {
	    if (!empty($appcode)) {
		$map['app_flag'] = $appcode;
	    }
	}
	if ($login_type == '2') {
	    $map['user_uid_invite'] = $admin_uid;
	}
	if (!empty($sex)) {
	    if ($sex == '3') {
//		$map['_complex'] = 'user_sex not in (\'1\',\'2\')';
		$map['user_sex'] = array(array('neq', '1'), array('neq', '2'), 'and');
	    } else {
		$map['user_sex'] = $sex;
	    }
	}
	if (!empty($idstate)) {
	    if ($idstate == '1') {
//		$map['_complex'] = 'state is null || state = \'\' || state = \'1\'';
		$map['state'] = array(array('exp', 'is null'), array('eq', ''), array('eq', '1'), 'or');
	    } else {
		$map['state'] = $idstate;
//		if ($idstate == '3') {
//		    if (!empty($flag)) {
//			if ($flag == '3') {
//			    $map['user_flag_switch'] = '2';
//			} else {
//			    $map['user_flag_switch'] = '1';
//			    $map['user_flag'] = $flag;
//			}
//		    }
//		}
	    }
	}
	if (!empty($recom)) {
	    $map['is_recommend'] = $recom;
	}
	if (!empty($active)) {
	    $map['is_active'] = $active;
	}
	if (!empty($hot)) {
	    $map['is_hot'] = $hot;
	}
	if (!empty($flag)) {
	    if ($flag == '3') {
		$map['user_flag_switch'] = '2';
	    } else {
		$map['user_flag_switch'] = '1';
		$map['user_flag'] = $flag;
	    }
	}
	if (!empty($agent)) {
	    $map['is_agent'] = $agent;
	}
	if (!empty($state)) {
	    $map['is_lock'] = $state;
	}
	if (!empty($begintime) || !empty($endtime)) {
	    if (!empty($begintime) && !empty($endtime)) {
		$map['ctime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
	    } else if (!empty($begintime)) {
		$map['ctime'] = array('between', array(strtotime($begintime), time() + 60));
	    } else if (!empty($endtime)) {
		$map['ctime'] = array('between', array(0, strtotime($endtime) + 60));
	    }
	}
	if (!empty($keyword)) {
	    $where['chat_userinfo.user_id'] = array('eq', $keyword);
	    $where['chat_userinfo.user_name'] = array('like', '%' . $keyword . '%');
	    $where['chat_userinfo.user_phone'] = array('like', '%' . $keyword . '%');
//	    $where['chat_userinfo.user_describe'] = array('like', '%' . $keyword . '%');
	    $where['_logic'] = 'or';
	    $map['_complex'] = $where;
	}
	$viewModel = new ViewModel\UserViewModel();
	$total = $viewModel->where($map)->count();
	$row = '10';
	$page = new Service\PageService($total, $row);
	$fpage = $page->fpage();
	$field = array(
	    'account', 'is_lock', 'ltime', 'id', 'user_id', 'user_uid', 'user_logo', 'user_name', 'user_sex', 'user_phone', 'user_birthday', 'user_profession', 'user_describe', 'user_tag', 'user_address', 'user_gold', 'user_flag', 'user_flag_switch', 'ctime', 'balance', 'state'
	);
	$data = $viewModel->where($map)->limit($page->listfirst, $page->listRows)->field($field)->order('ctime desc, id desc')->select();
	foreach ($data as $key => $value) {
	    $data[$key]['user_sex_text'] = empty($data[$key]['user_sex']) ? $sex_enum['3'] : $sex_enum[$data[$key]['user_sex']];
	    $data[$key]['user_gold_text'] = (int) $data[$key]['user_gold'];
	    $data[$key]['balance_text'] = (int) $data[$key]['balance'];
	    $data[$key]['user_flag_text'] = $data[$key]['state'] == '3' ? ($data[$key]['user_flag_switch'] == '1' ? $flag_enum[$data[$key]['user_flag']] : '禁止上架') : '';
	    $data[$key]['user_state_text'] = $state_enum[$data[$key]['is_lock']];
	    $data[$key]['is_agent_text'] = $yn_enum[$data[$key]['is_agent']];
	    $data[$key]['user_idstate_text'] = empty($data[$key]['state']) ? $id_state_enum['1'] : $id_state_enum[$data[$key]['state']];
	    $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
	    $data[$key]['ltime'] = date('Y-m-d H:i', $data[$key]['ltime']);
	    $data[$key]['ftime'] = date('Y-m-d H:i', $data[$key]['ftime']);
//	    $data[$key]['state_lab'] = $data[$key]['is_lock'] == '2' ? '解锁' : '锁定';
//	    $data[$key]['state_class'] = $data[$key]['is_lock'] == '2' ? 'btn-info' : 'btn-danger';
//	    $data[$key]['flag_lab'] = $data[$key]['user_flag'] == '2' ? '下架' : '上架';
//	    $data[$key]['flag_class'] = $data[$key]['user_flag'] == '2' ? 'btn-danger' : 'btn-info';
//	    $data[$key]['flag_style'] = $data[$key]['is_lock'] == '2' ? 'style="display:none;"' : ($data[$key]['user_flag_switch'] == '2' ? 'style="display:none;"' : '');
//	    $data[$key]['flag_switch_lab'] = $data[$key]['user_flag_switch'] == '2' ? '恢复' : '禁止上架';
//	    $data[$key]['flag_switch_class'] = $data[$key]['user_flag_switch'] == '2' ? 'btn-info' : 'btn-danger';
//	    $data[$key]['flag_switch_style'] = $data[$key]['is_lock'] == '2' ? 'style="display:none;"' : ($data[$key]['user_flag'] == '2' ? 'style="display:none;"' : '');
	    $data[$key]['agent_lab'] = $data[$key]['is_agent'] == '2' ? '撤销代理' : '成为代理';
	    $data[$key]['agent_class'] = $data[$key]['is_agent'] == '2' ? 'btn-danger' : 'btn-success';
	}
	$search = array('appsel' => $appcode, 'statesel' => $state, 'agentsel' => $agent, 'sexsel' => $sex, 'recomsel' => $recom, 'activesel' => $active, 'hotsel' => $hot, 'flagsel' => $flag, 'idstatesel' => $idstate, 'keyword' => $keyword, 'begintime' => $begintime, 'endtime' => $endtime);
	$this->assign(array(
	    'login_type' => $login_type,
	    'data' => $data,
	    'fpage' => $fpage,
	    'total' => $total,
	    'statearr' => $state_arr,
	    'agentarr' => $agent_arr,
	    'sexarr' => $sex_arr,
	    'recomarr' => $recom_arr,
	    'activearr' => $active_arr,
	    'hotarr' => $hot_arr,
	    'flagarr' => $flag_arr,
	    'idstatearr' => $id_state_arr,
	    'search' => $search,
	    'app_code' => $app_code,
	    'apparr' => $this->getapplist()
	));
	$this->display();
    }

    /**
     * 用户详情
     */
    public function userdetail() {
	$sex_enum = getenumbykey('sex');
	$id_state_enum = getenumbykey('useridentity_state');
	$yn_enum = getenumbykey('yn');
	$flag_enum = getenumbykey('flag');
	$state_enum = getenumbykey('admin_state');
	$user_uid = I('uid');
	$app_code = $this->app_code;
	$login_type = $this->login_type;
	$admin_uid = $this->admin_uid;
	$is_agent_self = (int) ($login_type == '2' && empty($user_uid));
	if ($login_type == '2') {
	    $user_uid = $admin_uid;
	}
	$map['user_uid'] = $user_uid;
	$field = array(
	    'account','online_state', 'is_lock', 'ltime', 'id', 'user_id', 'user_uid', 'user_logo', 'user_name', 'user_sex', 'user_phone', 'user_birthday','user_height','user_weight','user_tag', 'user_profession', 'user_describe', 'user_tag', 'user_address', 'user_tag', 'user_address', 'user_gold', 'user_flag', 'user_flag_switch', 'is_recommend', 'is_active', 'is_hot', 'user_vip', 'ftime', 'ctime', 'balance', 'img', 'state', 'ptime', 'id_ctime', 'app_flag'
	);
	$viewModel = new ViewModel\UserViewModel();
	$user = $viewModel->where($map)->field($field)->find();
	$user_vip=M('chat_vip')->where('id = '.$user['user_vip'])->field('id,vip')->find();
	$id = $user['id'];
	$user['balance'] = (int) $user['balance'];
	$user['user_name_text'] = empty($user['user_name']) ? $user['phone'] : $user['user_name'];
	$user['user_sex_text'] = empty($user['user_sex']) ? $sex_enum['3'] : $sex_enum[$user['user_sex']];
	$user['user_gold_text'] = (int) $user['user_gold'] . ' 金币';
	$user['balance_text'] = (int) $user['balance'] . ' 金币';
	$user['user_vip_text'] = empty($user_vip['vip']) ? '0 级' :  (int)$user_vip['vip'] . ' 星级';
	$user['is_recom_text'] = $yn_enum[$user['is_recommend']];
	$user['is_active_text'] = $yn_enum[$user['is_active']];
	$user['is_hot_text'] = $yn_enum[$user['is_hot']];
	$user['user_flag_text'] = $user['state'] == '3' ? ($user['user_flag_switch'] == '1' ? $flag_enum[$user['user_flag']] : '禁止上架') : '';
	$user['is_agent_text'] = $yn_enum[$user['is_agent']];
	$user['user_state_text'] = $state_enum[$user['is_lock']];
	$user['user_idstate_text'] = empty($user['state']) ? $id_state_enum['1'] : $id_state_enum[$user['state']];
	$user['user_birthday'] = empty($user['user_birthday']) ? '' : date('Y-m-d', $user['user_birthday']);
	$user['user_height_text'] =empty($user['user_height']) ? '0 cm' : $user['user_height'].' cm';
	$user['user_weight_text'] = empty($user['user_weight']) ? '0 kg' : $user['user_weight'].' kg';
	$user['ctime'] = date('Y-m-d H:i', $user['ctime']);
	$user['id_ctime'] = date('Y-m-d H:i', $user['id_ctime']);
	$user['online_state'] = C('USER_STATE')[$user['online_state']];


	//图片列表
	$viewModel = new ViewModel\UserimgsidentityViewModel();
	$field = array(
	    'user_id', 'user_uid', 'img', 'state', 'ptime', 'ctime'
	);
	$imgs_total = $viewModel->where($map)->count();
	$imgs = $viewModel->where($map)->field($field)->select();
    //视频列表
	$viewModel2 =new ViewModel\UservideosidentityViewModel();
	$field2 = array(
        'id','user_id',  'video', 'state', 'ptime', 'ctime'
    );
	$videos_total = $viewModel2->where($map)->count();
	$videos = $viewModel2->where($map)->field($field2)->select();
	$userinfoModel = M('chat_userinfo'); //邀请表
	$followModel = M('chat_userfollow'); //关注表
	$statistics['invite_sum'] = $userinfoModel->where(array('user_uid_parent' => $user_uid))->count();   //邀请人数
	$statistics['follow_sum'] = $followModel->where(array('user_uid' => $user_uid))->count();   //关注量
	$statistics['befollow_sum'] = $followModel->where(array('user_uid_follow' => $user_uid))->count();  //被关注量
	$statistics['invite_sum'] = empty($statistics['invite_sum']) ? '0' : (int) $statistics['invite_sum'];
	$statistics['follow_sum'] = empty($statistics['follow_sum']) ? '0' : (int) $statistics['follow_sum'];
	$statistics['befollow_sum'] = empty($statistics['befollow_sum']) ? '0' : (int) $statistics['befollow_sum'];
	$this->assign(array(
	    'login_type' => $login_type,
	    'is_agent_self' => $is_agent_self,
	    'id' => $id,
	    'uid' => $user_uid,
	    'user' => $user,
	    'imgs_total' => $imgs_total,
	    'imgs' => $imgs,
	    'videos_total' => $videos_total,
	    'videos' => $videos,
	    'statistics' => $statistics,
	    'app_code' => $app_code,
	));
	$this->display();
    }
    /**
     * 用户删除
     */
    public function userdelete() {
        
        $id = I('uid');
               if (!empty($id)) {
                   $model = M('chat_userinfo');
                   $result= $model->where(array('user_uid' => $id))->delete();
                   if ($result) {
                       return $this->ajaxReturn(array('ret' => '1001', 'code' => '删除成功！'));
                   } else {
                       return $this->ajaxReturn(array('ret' => '1002', 'code' => '删除失败！'));
                   }
               }
    }
    
    /**
     * 用户修改
     */
    public function useradd() {
	$btnname = '添加';
	$columnname = '添加用户';
	$sex_enum = getenumbykey('sex');
	$sex_arr = getarrbyenum($sex_enum);
	$yn_enum = getenumbykey('yn');
	$recom_arr = getarrbyenum($yn_enum);
	$active_arr = getarrbyenum($yn_enum);
	$hot_arr = getarrbyenum($yn_enum);
	$agent_arr = getarrbyenum($yn_enum);
	$flag_enum = getenumbykey('flag');
	$flag_arr = getarrbyenum($flag_enum);
	$state_enum = getenumbykey('userlogin_state');
	$state_arr = getarrbyenum($state_enum);
	$user_uid = I('uid');
	$login_type = $this->login_type;
	$app_code = $this->app_code;
	$admin_uid = $this->admin_uid;
	$is_agent_self = (int) ($login_type == '2' && empty($user_uid));
        $vip_arr = M('chat_vip')->select();//主播星级
	if ($login_type == '2') {
	    $user_uid = $admin_uid;
	}
	if (!empty($user_uid)) {
	    $columnname = '修改用户';
	    $btnname = '修改';
	    $map['user_uid'] = $user_uid;
	    $viewModel = new ViewModel\UserViewModel();
	    $data = $viewModel->where($map)->find();
	    $id = $data['id'];
	    $code = $data['code'];
	    $user_phone = $data['user_phone'];
	    $user_logo = $data['user_logo'];
	    $user_name = $data['user_name'];
	    $user_sex = empty($data['user_sex']) ? '3' : $data['user_sex'];
	    $user_birthday = empty($data['user_birthday']) ? '' : date('Y-m-d', $data['user_birthday']);
	    $user_profession = $data['user_profession'];
	    $user_describe = $data['user_describe'];
	    $user_tag = $data['user_tag'];
	    $user_address = $data['user_address'];
	    $user_gold = $data['user_gold'];
	    $balance = $data['balance'];
        $user_vip = $data['user_vip'];
	    $user_flag = $data['user_flag'];
	    $user_flag_switch = $data['user_flag_switch'];
	    $is_recommend = $data['is_recommend'];
	    $is_active = $data['is_active'];
	    $is_hot = $data['is_hot'];
	    $state = $data['is_lock'];
	    $is_agent = empty($data['is_agent']) ? '1' : $data['is_agent'];
	    $ftime = date('Y-m-d H:i', $data['ftime']);
	    $ctime = date('Y-m-d H:i', $data['ctime']);
	    $data['user_gold'] = (int) $user_gold;
	    $data['balance'] = (int) $balance;
	    $data['user_birthday'] = $user_birthday;
	    $data['user_state_text'] = $state_enum[$state];

	}


	foreach ($vip_arr as $key =>$value){
	    $vip_arr[$key]['text'] = $vip_arr[$key]['vip'].' 星级';
        if($user_vip== $value['vip']) {
            $vip_arr[$key]['selected'] = "selected='selected'";
        }
    }
	foreach ($agent_arr as $key => $value) {
	    if ($is_agent == $value['value']) {
		$agent_arr[$key]['selected'] = "selected='selected'";
	    }
	}
	foreach ($sex_arr as $key => $value) {
	    if ($user_sex == $value['value']) {
		$sex_arr[$key]['selected'] = "selected='selected'";
	    }
	}
	foreach ($flag_arr as $key => $value) {
	    if ($user_flag == $value['value']) {
		$flag_arr[$key]['selected'] = "selected='selected'";
	    }
	}
	foreach ($recom_arr as $key => $value) {
	    if ($is_recommend == $value['value']) {
		$recom_arr[$key]['selected'] = "selected='selected'";
	    }
	}
	foreach ($active_arr as $key => $value) {
	    if ($is_active == $value['value']) {
		$active_arr[$key]['selected'] = "selected='selected'";
	    }
	}
	foreach ($hot_arr as $key => $value) {
	    if ($is_hot == $value['value']) {
		$hot_arr[$key]['selected'] = "selected='selected'";
	    }
	}
	// 在线状态
    $state_list = [];
    foreach ( C('USER_STATE') as $key => $value) {
        $state_list[] = ['key'=>$key,'value'=>$value];
       if ($data['online_state'] == $key) {
           $state_list[] = ['key'=>$key,'value'=>$value,'selected'=>"selected='selected'"];
       }
     }


	$this->assign(array(
	    'login_type' => $login_type,
	    'is_agent_self' => $is_agent_self,
	    'id' => $id,
	    'uid' => $user_uid,
	    'phone' => $user_phone,
	    'data' => $data,
	    'ftime' => $ftime,
	    'ctime' => $ctime,
	));
	$this->assign('columnname', $columnname);
	$this->assign('btnname', $btnname);
	$this->assign('agentarr', $agent_arr);
	$this->assign('sexarr', $sex_arr);
	$this->assign('flagarr', $flag_arr);
	$this->assign('recomarr', $recom_arr);
	$this->assign('activearr', $active_arr);
	$this->assign('hotarr', $hot_arr);
	$this->assign('app_code', $app_code);
	$this->assign('viparr',$vip_arr);
	$this->assign('state_list',$state_list);
	$this->display();
    }

    /**
     * 用户修改提交
     * @return type
     */
    public function useraddsubmit() {
	if (!IS_POST) {
	    $this->error('非法访问！');
	}
	$app_code = $this->app_code;
	$user_uid = I('uid');
	$img = trim(I('img'));
	$name = trim(I('name'));
	$sex = trim(I('sex'));
	$agent = trim(I('agent'));
	$gold = trim(I('gold'));
	$balance = trim(I('balance'));
	$birthday = trim(I('birthday'));
	$profession = trim(I('profession'));
	$describe = trim(I('describe'));
	$address = trim(I('address'));
	$flag = trim(I('flag'));
	$recom = trim(I('recom'));
	$active = trim(I('active'));
	$hot = trim(I('hot'));
	$vip = trim(I('vip'));
	$height = trim(I('height'));
	$weight = trim(I('weight'));
	$user_tag = trim(I('user_tag'));
	$state_list = trim(I('state_list'));
//	if (empty($img)) {
//	    $response = array('ret' => '1002', 'code' => '图片不能为空！');
//	    return $this->ajaxReturn($response, 'json');
//	}
//	if (empty($name)) {
//	    $response = array('ret' => '1002', 'code' => '名称不能为空！');
//	    return $this->ajaxReturn($response, 'json');
//	}
	if (empty($gold)) {
	    $gold = 0;
	}
	if (empty($flag)) {
	    $flag = '1';
	}
	if (empty($recom)) {
	    $recom = '1';
	}
	if (empty($active)) {
	    $active = '1';
	}
	if (empty($hot)) {
	    $hot = '1';
	}
	if(empty($vip)|| (int)$vip < 0){
	    $vip = '0';
    }
    if($height < 0 && $height>299){
        $response = array('ret' => '1002', 'code' => '无效的身高');
        return $this->ajaxReturn($response, 'json');
    }
    if($weight < 0 && $weight>199){
        $response = array('ret' => '1002', 'code' => '无效的体重！');
        return $this->ajaxReturn($response, 'json');
    }
	$data['user_logo'] = $img;
	$data['user_name'] = $name;
	$data['user_sex'] = $sex;
	$data['user_gold'] = (int) $gold;
	$data['user_birthday'] = empty($birthday) ? '0' : strtotime($birthday);
	$data['user_profession'] = $profession;
	$data['user_describe'] = $describe;
	$data['user_address'] = $address;
	$data['user_height'] = $height;
	$data['user_weight'] = $weight;
	$data['user_vip'] = $vip;
	$data['is_recommend'] = $recom;
	$data['is_active'] = $active;
	$data['is_hot'] = $hot;
	$data['user_flag'] = $flag;
	$data['user_tag'] = $user_tag;
	$data['state'] = $state_list;

	if (empty($user_uid)) {
	    $data['ctime'] = time();
	}
	if (!empty($user_uid)) {
	    $map['user_uid'] = $user_uid;
	    $viewModel = new ViewModel\UserViewModel();
	    $user = $viewModel->where($map)->find();
	    if ($user) {
		$balance_bak = (int) $user['balance'];
		$balance = (int) $balance;
		$balance_diff = $balance - $balance_bak;
		$balance_diff = 0;

		//是否主播
        if($user['user_auth'] <> 2){
            $data['user_vip'] = 0;
        }
		//用户信息更新
//		$model = M('chat_userlogin');
//		$result = $model->where($map)->save(array('is_agent' => $agent));
		$model = M('chat_userinfo');
//		newredishmset(33, "userinfo_" . $user_uid, $data);
		$result = $model->where($map)->save($data);
		if (abs($balance_diff) > 0) {
		    //账户余额更新
		    $model = M('chat_userwallet');
		    $model->where($map)->save(array('balance' => $balance));
		    //添加账户余额更新日志
		    $system = M('chat_systemset')->order('id desc')->find();
		    $ratio = empty($system['ratio']) ? '0' : $system['ratio'];
		    $model = M('chat_balancelog');
		    if ($balance_diff > 0) {
			$balance_diff = abs($balance_diff);
			$data = array(
			    'user_uid' => $user_uid,
			    'order_no' => 'QT' . time() . rand(100000, 999999),
			    'content' => '管理员手动添加 ' . $balance_diff . ' 金币',
			    'money' => $balance_diff,
			    'balance' => $balance,
			    'ratio' => $ratio,
			    'state' => '1',
			    'type' => '5',
			    'toggle' => '2',
			    'ctime' => time(),
			    'app_flag' => $app_code,
			);
		    } else {
			$balance_diff = abs($balance_diff);
			$data = array(
			    'user_uid' => $user_uid,
			    'order_no' => 'QT' . time() . rand(100000, 999999),
			    'content' => '管理员手动减掉 ' . $balance_diff . ' 金币',
			    'money' => $balance_diff,
			    'balance' => $balance,
			    'ratio' => $ratio,
			    'state' => '1',
			    'type' => '5',
			    'toggle' => '1',
			    'ctime' => time(),
			    'app_flag' => $app_code,
			);
		    }
//		    newredisadd(33, "userwallet_" . $user_uid, 'balance', $balance);
		    $model->add($data);
		}
		if ($result) {
		    $response = array('ret' => '1001', 'code' => '修改成功！');
		} else {
		    $response = array('ret' => '1001', 'code' => '修改成功！');
		}
	    } else {
		$response = array('ret' => '1002', 'code' => '对不起，找不到该用户！');
	    }
	} else {
	    $response = array('ret' => '1002', 'code' => '无效的操作！');
	}
	return $this->ajaxReturn($response, 'json');
    }

    public function userbalanceeditsubmit() {
	$user_uid = I('uid');
	$balance = I('balance');
	$balanceremark = I('balanceremark');
	$appcode = trim(I('ddlapp'));
    $app_code = $this->app_code;
//    if (!empty($app_code)) {
//        $map['app_flag'] = $app_code;
//    } else {
//        if (!empty($appcode)) {
//            $map['app_flag'] = $appcode;
//        }
//    }
	if (!empty($user_uid)) {
	    $map['user_uid'] = $user_uid;
	    $model = M('chat_userwallet');
	    $wallet = $model->where($map)->find();
	    if ($wallet) {
		$balance_bak = $wallet['balance'];
		$balance = $balance;
		$balance_diff = $balance - $balance_bak;
		if (abs($balance_diff) > 0) {
		    //账户余额更新
		    $model->where($map)->save(array('balance' => $balance));
		    //添加账户余额更新日志
		    $system = M('chat_systemset')->order('id desc')->field('ratio')->find();
		    $ratio = empty($system['ratio']) ? '0' : $system['ratio'];
		    $model = M('chat_balancelog');
		    if ($balance_diff > 0) {
			$balance_diff = abs($balance_diff);
			$data = array(
			    'user_uid' => $user_uid,
			    'order_no' => 'QT' . time() . rand(100000, 999999),
			    'content' => empty($balanceremark) ? '管理员手动添加 ' . $balance_diff . ' 金币' : $balanceremark,
			    'money' => $balance_diff,
			    'balance' => $balance,
			    'ratio' => $ratio,
			    'state' => '1',
			    'type' => '5',
			    'toggle' => '2',
			    'ctime' => time(),
			    'app_flag' => $app_code,
			);
		    } else {
			$balance_diff = abs($balance_diff);
			$data = array(
			    'user_uid' => $user_uid,
			    'order_no' => 'QT' . time() . rand(100000, 999999),
			    'content' => empty($balanceremark) ? '管理员手动减掉 ' . $balance_diff . ' 金币' : $balanceremark,
			    'money' => $balance_diff,
			    'balance' => $balance,
			    'ratio' => $ratio,
			    'state' => '1',
			    'type' => '5',
			    'toggle' => '1',
			    'ctime' => time(),
			    'app_flag' => $app_code,
			);
		    }
		    //--临时注释掉
		    //newredisadd(33, "userwallet_" . $user_uid, 'balance', $balance);
		    $model->add($data);

		    $response = array('ret' => '1001', 'code' => '修改成功！');
		} else {
		    $response = array('ret' => '1002', 'code' => '未做修改！');
		}
	    } else {
		$response = array('ret' => '1002', 'code' => '对不起，找不到该用户钱包！');
	    }
	} else {
	    $response = array('ret' => '1002', 'code' => '无效的操作！');
	}
	return $this->ajaxReturn($response, 'json');
    }

    /**
     * 用户收支明细
     */
    public function userbalancedetail() {
	$user_uid = I('uid');
	$map['user_uid'] = $user_uid;
	//账户余额
	$field = array(
	    'user_id', 'user_uid', 'user_phone', 'is_lock', 'ctime', 'user_logo', 'user_name', 'user_sex', 'user_birthday', 'user_profession', 'user_address', 'user_describe', 'user_tag', 'user_gold', 'balance', 'user_flag', 'user_flag_switch', 'is_recommend', 'is_active', 'is_hot', 'ftime', 'img', 'state', 'ptime'
	);
	$viewModel = new ViewModel\UserViewModel();
	$user = $viewModel->where($map)->field($field)->find();
	$balance = (int) $user['balance'];
	//收支明细合计
	$balancelogModel = M('chat_balancelog'); //账户余额日志表
//	$data['money_out_sum'] = $balancelogModel->where($map)->where(array('state' => '1', 'type' => '1'))->sum('money');  //消费金额（金币）
//	$data['money_in_sum'] = $balancelogModel->where($map)->where(array('state' => '1', 'type' => '3'))->sum('money');  //收益金额（金币）
//	$data['money_recharge_sum'] = $balancelogModel->where($map)->where(array('state' => '1', 'type' => '2'))->sum('money');  //充值金额（金币）
//	$data['money_withdraw_sum'] = $balancelogModel->where($map)->where(array('state' => '1', 'type' => '4'))->sum('money');  //提现金额（金币）
//	$data['money_other_sum'] = $balancelogModel->where($map)->where(array('state' => '1', 'type' => '5'))->sum('money');  //其他金额（管理员更改等）（金币）
	$field = 'type, sum(money) as total';
	$group = 'type';
	$balancelog_sum = $balancelogModel->where($map)->where(array('state' => '1'))->field($field)->group($group)->select();
	foreach ($balancelog_sum as $key => $value) {
	    switch ($value['type']) {
		case '1':
		    $data['money_out_sum'] = $value['total'];
		    break;
		case '2':
		    $data['money_recharge_sum'] = $value['total'];
		    break;
		case '3':
		    $data['money_in_sum'] = $value['total'];
		    break;
		case '4':
		    $data['money_withdraw_sum'] = $value['total'];
		    break;
		case '5':
		    $data['money_other_sum'] = $value['total'];
		    break;
		default:
		    break;
	    }
	}
	$data['money_out_sum'] = empty($data['money_out_sum']) ? '0' : (int) $data['money_out_sum'];
	$data['money_in_sum'] = empty($data['money_in_sum']) ? '0' : (int) $data['money_in_sum'];
	$data['money_recharge_sum'] = empty($data['money_recharge_sum']) ? '0' : (int) $data['money_recharge_sum'];
	$data['money_withdraw_sum'] = empty($data['money_withdraw_sum']) ? '0' : (int) $data['money_withdraw_sum'];
	$data['money_other_sum'] = empty($data['money_other_sum']) ? '0' : (int) $data['money_other_sum'];
	$this->assign(array(
	    'uid' => $user_uid,
	    'balance' => $balance,
	    'data' => $data,
	));
	$this->display();
    }

    /**
     * 用户收支（消费/充值/赚取/提现）明细表
     * @return type
     */
    public function getmessagedata() {
	if (!IS_POST) {
	    $this->error('非法访问！');
	}
	$type_enum = getenumbykey('balancelog_type');
	$toggle_enum = getenumbykey('balancelog_toggle');
	$page_size = I('pagesize', '5');
	$page = I('page', '0');
	$page_index = ($page - 1) * $page_size;
	$user_uid = I('uid');
	$type = I('type');
	$map['state'] = '1';
	$map['user_uid'] = $user_uid;
	if (!empty($type)) {
	    $map['type'] = $type;
	}
	$field = array(
	    'id', 'user_id', 'user_uid', 'user_name', 'money', 'balance', 'state', 'type', 'toggle', 'content', 'ctime'
	);
	$viewModel = new ViewModel\BalancelogViewModel();
	$total = $viewModel->where($map)->count();
	$data = $viewModel->where($map)->limit($page_index, $page_size)->field($field)->order('ctime desc')->select();
	foreach ($data as $key => $value) {
	    $content = $data[$key]['content'];
	    $strlen = mb_strlen($content, 'utf8');
	    if (!empty($content) && $strlen > 16) {
		$data[$key]['content'] = mb_substr($content, 0, 8, 'utf8') . '…' . mb_substr($content, $strlen - 8, $strlen, 'utf8');
	    }
	    $data[$key]['money'] = (int) $data[$key]['money'];
	    if ($data[$key]['type'] == '5') {
		$data[$key]['money'] = ($data[$key]['toggle'] == '1' ? '-' : '+') . (int) $data[$key]['money'];
	    }
//	    $data[$key]['money'] = ($data[$key]['toggle'] == '1' ? '-' : '+') . (int) $data[$key]['money'];
	    $data[$key]['money_in'] = $data[$key]['toggle'] == '1' ? '' : (int) $data[$key]['money'];
	    $data[$key]['money_out'] = $data[$key]['toggle'] == '1' ? (int) $data[$key]['money'] : '';
	    $data[$key]['balance'] = (int) $data[$key]['balance'];
	    $data[$key]['type'] = $type_enum[$data[$key]['type']];
	    $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
	}
//	$response = $data;
	$response = array('ret' => '1001', 'total' => $total, 'data' => $data);
	return $this->ajaxReturn($response, 'json');
    }

    /**
     * 推荐/活跃/排行用户列表
     */
    public function userrahlist() {
	$sex_enum = getenumbykey('sex');
	$sex_arr = getarrbyenum($sex_enum);
    $grade_enum = getenumbykey('anchor_grade_state');
    $grade_arr = getarrbyenum($grade_enum);
	$app_code = $this->app_code;
	$type = trim(I('type'));
	$sex = trim(I('ddlsex'));
    $grade = trim(I('ddlgrade'));
	$begintime = trim(I('txtbegintime'));
	$endtime = trim(I('txtendtime'));
	$keyword = trim(I('txtkeyword'));
	switch ($type) {
	    case 'recommend':
		$columnname = '推荐用户列表';
		break;
	    case 'active':
		$columnname = '活跃用户列表';
		break;
//	    case 'hot':
//		$columnname = '排行用户列表';
//		break;
	    default:
		break;
	}
	$appcode = trim(I('ddlapp'));

	if (!empty($app_code)) {
	    $map['app_flag'] = $app_code;
	} else {
	    if (!empty($appcode)) {
		$map['app_flag'] = $appcode;
	    }
	}
	$map['is_lock'] = '1';
//	$map['state'] = '3';
    $map['user_auth'] = '2';
	$map['user_flag_switch'] = '1';
	$map['user_flag'] = '2';
	if (!empty($type)) {
	    $map['is_' . $type] = '2';
	}
	if (!empty($sex)) {
	    if ($sex == '3') {
		$map['user_sex'] = array(array('neq', '1'), array('neq', '2'), 'and');
	    } else {
		$map['user_sex'] = $sex;
	    }
	}
	if(empty($grade)){
        $grade = 100;
    }
        if ((!empty((int)$grade) || $grade == 0) && $grade != 100){
            $map['user_vip'] = $grade;
        }
	if (!empty($begintime) || !empty($endtime)) {
	    if (!empty($begintime) && !empty($endtime)) {
		$map['ctime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
	    } else if (!empty($begintime)) {
		$map['ctime'] = array('between', array(strtotime($begintime), time() + 60));
	    } else if (!empty($endtime)) {
		$map['ctime'] = array('between', array(0, strtotime($endtime) + 60));
	    }
	}
	if (!empty($keyword)) {
	    $where['chat_userinfo.user_id'] = array('eq', $keyword);
	    $where['chat_userinfo.user_name'] = array('like', '%' . $keyword . '%');
	    $where['chat_userinfo.user_phone'] = array('like', '%' . $keyword . '%');
	    $where['chat_userinfo.user_profession'] = array('like', '%' . $keyword . '%');
	    $where['chat_userinfo.user_describe'] = array('like', '%' . $keyword . '%');
	    $where['_logic'] = 'or';
	    $map['_complex'] = $where;
	}
	$viewModel = new ViewModel\UserViewModel();
	$total = $viewModel->where($map)->count();
	$row = '10';
	$page = new Service\PageService($total, $row);
	$fpage = $page->fpage();
	$field = array(
	    'user_id', 'user_uid', 'ctime', 'user_phone', 'is_lock', 'ltime', 'balance', 'user_logo', 'user_name', 'user_sex', 'user_birthday', 'user_profession', 'user_describe', 'user_tag', 'user_gold', 'user_flag', 'user_flag_switch', 'is_recommend', 'is_active', 'is_hot', 'ftime', 'state', 'user_auth','app_flag','user_call_time','total_online_time','user_vip'
	);
	$order = 'is_hot desc,fans_num desc, ftime desc, ctime desc, id desc';
	$data = $viewModel->where($map)->limit($page->listfirst, $page->listRows)->field($field)->order($order)->select();
        $vip = M('chat_vip')->field('id,vip')->order('vip desc')->select();
	foreach ($data as $key => $value) {
	    $data[$key]['rah_state'] = $data[$key]['is_' . $type];
	    $data[$key]['user_sex_text'] = empty($data[$key]['user_sex']) ? $sex_enum['3'] : $sex_enum[$data[$key]['user_sex']];
	    $data[$key]['user_gold_text'] = (int) $data[$key]['user_gold'];
	    $data[$key]['balance_text'] = (int) $data[$key]['balance'];
	    $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
	    $data[$key]['ltime'] = date('Y-m-d H:i', $data[$key]['ltime']);
	    $data[$key]['ftime'] = date('Y-m-d H:i', $data[$key]['ftime']);

        $call_time_txt = $data[$key]['user_call_time']>0 ? secToTime($data[$key]['user_call_time']) : '0';
        $online_time_txt = $data[$key]['total_online_time'] >0 ? secToTime($data[$key]['total_online_time']) : '0';
        if(($data[$key]['user_vip'] > 0) && ($data[$key]['user_sex'] != 1)){
            foreach ($vip as $k =>$v){
                if($data[$key]['user_vip'] == $v['id']){
                    $vip_text=$v['vip'].'星级';
                }
            }}elseif(($data[$key]['user_vip']<1 && ($data[$key]['user_sex'] != 1)) ){
            $vip_text='新人';
        }elseif(!($data[$key]['user_sex'] == 1)){
            $vip_text='';
        };
        $is_hot=$data[$key]['is_hot'];
        $data[$key]['user_information'] = "主播等级:$vip_text;热度:$is_hot <br />在线时长:$call_time_txt  <br />通话时长:$online_time_txt";
        $vip_text='';
        $call_time_txt='';
        $online_time_txt='';
	}
	$search = array('appsel' => $appcode, 'sexsel' => $sex, 'gradesel' => $grade, 'keyword' => $keyword, 'begintime' => $begintime, 'endtime' => $endtime);
	$this->assign(array(
	    'data' => $data,
	    'fpage' => $fpage,
	    'total' => $total,
	    'sexarr' => $sex_arr,
	    'search' => $search,
        'gradearr' => $grade_arr,
	    'type' => $type,
	    'columnname' => $columnname,
	    'app_code' => $app_code,
	    'apparr' => $this->getapplist()
	));
	$this->display();
    }

    /*
     * 主播热度修改提交
     * user_id主播短id
     * is_hot 主播热度
     */
    public function usereditishot(){
        $ishot= (int)trim(I('ishot'));
        $user_id=trim(I('user_id'));
        $map['user_auth']=2;
        $map['user_id']=$user_id;
        $model=M('chat_userinfo');
        $is_hot=$model->where($map)->getField('is_hot');
        if($ishot==$is_hot){return $this->ajaxReturn(array('ret' => '1002', 'code' => '请不要重复更新热度'));}
        $isok = $model->where($map)->setField(array('is_hot'=>$ishot));
        if($isok){
            return $this->ajaxReturn(array('ret' => '1001', 'code' => '更新成功'));
        }else{
            return $this->ajaxReturn(array('ret' => '1002', 'code' => '更新失败'));
        }
    }
    /**
     * 用户添加推荐/活跃/排行提交
     * @return type
     */
    public function userrahaddsubmit() {
	if (!IS_POST) {
	    $this->error('非法访问！');
	}
	$app_code = I('app_code');
	$type = I('type');
	$id = I('id');
	if (!empty($id)) {
	    $map['user_id'] = $id;
//	    $map['app_flag'] = $app_code;
	    $viewModel = new ViewModel\UserViewModel();
	    $data = $viewModel->where($map)->find();
	    $user_uid = $data['user_uid'];
	    $map = array();
	    $map['user_uid'] = $user_uid;
	    $model = M('chat_userinfo');
	    if ($data) {
		$state = $data['is_' . $type];
		$is_lock = $data['is_lock'];
		$idstate = $data['state'];
		$user_flag = $data['user_flag'];
		$user_flag_switch = $data['user_flag_switch'];
		if ($state == '2' && $user_flag_switch == '1' && $user_flag == '2') {
		    return $this->ajaxReturn(array('ret' => '1001', 'code' => '该用户已添加！'));
		}
		$state = '2';
		$lab = '添加';
		if ($state == '2') {
		    if ($is_lock == '2') {
			return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该用户已被锁定！'));
		    }
		    if ($idstate != '3') {
			return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该用户还不是主播！'));
		    }
		    if ($user_flag_switch == '2') {
			return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该用户已被禁止上架！'));
		    }
		}
//		newredishmset(33, "userinfo_" . $user_uid, array('is_' . $type => $state, 'user_flag_switch' => '1', 'user_flag' => '2', 'ftime' => time()));
		$result = $model->where($map)->save(array('is_' . $type => $state, 'user_flag_switch' => '1', 'user_flag' => '2', 'ftime' => time()));
		if ($result) {
		    return $this->ajaxReturn(array('ret' => '1001', 'code' => $lab . '成功！', 'data' => $state));
		} else {
		    return $this->ajaxReturn(array('ret' => '1002', 'code' => $lab . '失败！', 'data' => $state));
		}
	    } else {
		return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，找不到该用户！'));
	    }
	} else {
	    return $this->ajaxReturn(array('ret' => '1002', 'code' => '无效的操作！'));
	}
    }

    /**
     * 用户推荐/活跃/排行提交
     * @return type
     */
    public function userrahsubmit() {
	if (!IS_POST) {
	    $this->error('非法访问！');
	}
	$app_code = I('app_code');
	$type = I('type');
	$user_uid = I('uid');
	if (!empty($user_uid)) {
	    $map['user_uid'] = $user_uid;
	    $viewModel = new ViewModel\UserViewModel();
	    $data = $viewModel->where($map)->find();
	    $model = M('chat_userinfo');
	    if ($data) {
		$is_lock = $data['is_lock'];
		$user_flag = $data['user_flag'];
		$user_flag_switch = $data['user_flag_switch'];
		$state = $data['is_' . $type] == '1' ? '2' : '1';
		$lab = $state == '1' ? '取消' : '添加';
		if ($state == '2') {
		    if ($is_lock == '2') {
			return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该用户已被锁定！'));
		    }
		    if ($user_flag_switch == '2') {
			return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该用户已被禁止上架！'));
		    }
		}
		$save = array('is_' . $type => $state, 'user_flag_switch' => '1', 'user_flag' => '2');
		if ($user_flag != '2') {
		    $save['ftime'] = time();
		}
//		newredishmset(33, "userinfo_" . $user_uid, $save);
		$result = $model->where($map)->save(array('is_' . $type => $state, 'user_flag_switch' => '1', 'user_flag' => '2', 'ftime' => time()));
		if ($result) {
		    return $this->ajaxReturn(array('ret' => '1001', 'code' => $lab . '成功！', 'data' => $state));
		} else {
		    return $this->ajaxReturn(array('ret' => '1002', 'code' => $lab . '失败！', 'data' => $state));
		}
	    } else {
		return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，找不到该用户！'));
	    }
	} else {
	    return $this->ajaxReturn(array('ret' => '1002', 'code' => '无效的操作！'));
	}
    }

    /**
     * 代理用户列表
     */
    public function useragentlist() {
	$columnname = '代理用户列表';
	$sex_enum = getenumbykey('sex');
	$sex_arr = getarrbyenum($sex_enum);
	$id_state_enum = getenumbykey('useridentity_state');
	$id_state_arr = getarrbyenum($id_state_enum);
	$flag_enum = getenumbykey('flag');
	$flag_arr = getarrbyenum($flag_enum);
	$state_enum = getenumbykey('userlogin_state');
	$state_arr = getarrbyenum($state_enum);
	$app_code = $this->app_code;
	$type = trim(I('type'));
	$sex = trim(I('ddlsex'));
	$idstate = trim(I('ddlidstate'));
	$flag = trim(I('ddlflag'));
	$begintime = trim(I('txtbegintime'));
	$endtime = trim(I('txtendtime'));
	$keyword = trim(I('txtkeyword'));
	$searchtype = trim(I('hidsearchtype'));
	switch ($searchtype) {
	    case '1':

		break;
	    case '2':
		if ($idstate == '3') {
		    
		} else {
		    $flag = '0';
		}
		break;
	    case '3':
		if (!empty($flag)) {
		    $idstate = '3';
		}
		break;
	    case '4':

		break;
	    default:
		break;
	}
	$appcode = trim(I('ddlapp'));

	if (!empty($app_code)) {
	    $map['app_flag'] = $app_code;
	} else {
	    if (!empty($appcode)) {
		$map['app_flag'] = $appcode;
	    }
	}
	$map['is_lock'] = '1';
	$map['is_agent'] = '1';
	if ($type == '2') {
	    $map['is_agent'] = '2';
	}
	if (!empty($sex)) {
	    if ($sex == '3') {
		$map['user_sex'] = array(array('neq', '1'), array('neq', '2'), 'and');
	    } else {
		$map['user_sex'] = $sex;
	    }
	}
	if (!empty($idstate)) {
	    if ($idstate == '1') {
		$map['state'] = array(array('exp', 'is null'), array('eq', ''), array('eq', '1'), 'or');
	    } else {
		$map['state'] = $idstate;
//		if ($idstate == '3') {
//		    if (!empty($flag)) {
//			if ($flag == '3') {
//			    $map['user_flag_switch'] = '2';
//			} else {
//			    $map['user_flag_switch'] = '1';
//			    $map['user_flag'] = $flag;
//			}
//		    }
//		}
	    }
	}
	if (!empty($flag)) {
	    if ($flag == '3') {
		$map['user_flag_switch'] = '2';
	    } else {
		$map['user_flag_switch'] = '1';
		$map['user_flag'] = $flag;
	    }
	}
	if ($type == '2') {
	    if (!empty($begintime) || !empty($endtime)) {
		if (!empty($begintime) && !empty($endtime)) {
		    $map['atime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
		} else if (!empty($begintime)) {
		    $map['atime'] = array('between', array(strtotime($begintime), time() + 60));
		} else if (!empty($endtime)) {
		    $map['atime'] = array('between', array(0, strtotime($endtime) + 60));
		}
	    }
	} else {
	    if (!empty($begintime) || !empty($endtime)) {
		if (!empty($begintime) && !empty($endtime)) {
		    $map['ctime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
		} else if (!empty($begintime)) {
		    $map['ctime'] = array('between', array(strtotime($begintime), time() + 60));
		} else if (!empty($endtime)) {
		    $map['ctime'] = array('between', array(0, strtotime($endtime) + 60));
		}
	    }
	}
	if (!empty($keyword)) {
	    $where['chat_userinfo.user_id'] = array('eq', $keyword);
	    $where['chat_userinfo.user_name'] = array('like', '%' . $keyword . '%');
	    $where['chat_userinfo.user_phone'] = array('like', '%' . $keyword . '%');
	    $where['chat_userinfo.user_profession'] = array('like', '%' . $keyword . '%');
	    $where['chat_userinfo.user_describe'] = array('like', '%' . $keyword . '%');
	    $where['_logic'] = 'or';
	    $map['_complex'] = $where;
	}
	$viewModel = new ViewModel\UserViewModel();
	$total = $viewModel->where($map)->count();
	$row = '10';
	$page = new Service\PageService($total, $row);
	$fpage = $page->fpage();
	$field = array(
	    'user_id', 'user_uid', 'ctime', 'user_phone', 'is_lock', 'ltime', 'is_agent', 'atime', 'balance', 'user_logo', 'user_name', 'user_sex', 'user_birthday', 'user_profession', 'user_describe', 'user_tag', 'user_gold', 'user_flag', 'user_flag_switch', 'is_recommend', 'is_active', 'is_hot', 'ftime', 'state'
	);
	$order = 'ctime desc, id desc';
	if ($type == '2') {
	    $order = 'atime desc, ctime desc, id desc';
	}
	$data = $viewModel->where($map)->limit($page->listfirst, $page->listRows)->field($field)->order($order)->select();
	foreach ($data as $key => $value) {
	    $data[$key]['user_sex_text'] = empty($data[$key]['user_sex']) ? $sex_enum['3'] : $sex_enum[$data[$key]['user_sex']];
	    $data[$key]['user_gold_text'] = (int) $data[$key]['user_gold'];
	    $data[$key]['balance_text'] = (int) $data[$key]['balance'];
	    $data[$key]['user_flag_text'] = $data[$key]['state'] == '3' ? ($data[$key]['user_flag_switch'] == '1' ? $flag_enum[$data[$key]['user_flag']] : '禁止上架') : '';
	    $data[$key]['user_state_text'] = $state_enum[$data[$key]['is_lock']];
	    $data[$key]['user_idstate_text'] = empty($data[$key]['state']) ? $id_state_enum['1'] : $id_state_enum[$data[$key]['state']];
	    $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
	    $data[$key]['atime'] = date('Y-m-d H:i', $data[$key]['atime']);
	    $data[$key]['ftime'] = date('Y-m-d H:i', $data[$key]['ftime']);
	    $data[$key]['agent_lab'] = $data[$key]['is_agent'] == '2' ? '撤销代理' : '成为代理';
	    $data[$key]['agent_class'] = $data[$key]['is_agent'] == '2' ? 'btn-danger' : 'btn-success';
	}
	$search = array('appsel' => $appcode, 'sexsel' => $sex, 'flagsel' => $flag, 'idstatesel' => $idstate, 'keyword' => $keyword, 'begintime' => $begintime, 'endtime' => $endtime);
	$this->assign(array(
	    'data' => $data,
	    'fpage' => $fpage,
	    'total' => $total,
	    'sexarr' => $sex_arr,
	    'flagarr' => $flag_arr,
	    'idstatearr' => $id_state_arr,
	    'search' => $search,
	    'type' => $type,
	    'columnname' => $columnname,
	    'app_code' => $app_code,
	    'apparr' => $this->getapplist()
	));
	$this->display();
    }

    /**
     * 成为/撤销代理提交
     * @return type
     */
    public function useragentsubmit() {
	if (!IS_POST) {
	    $this->error('非法访问！');
	}
	$user_uid = I('uid');
	if (!empty($user_uid)) {
	    $map['user_uid'] = $user_uid;
	    $viewModel = new ViewModel\UserViewModel();
	    $data = $viewModel->where($map)->find();
	    $model = M('chat_userlogin');
	    if ($data) {
		$is_lock = $data['is_lock'];
		$phone = $data['phone'];
		$state = $data['is_agent'] == '1' ? '2' : '1';
		$lab = $state == '1' ? '撤销代理' : '成为代理';
		if ($state == '2') {
		    $password = md5('123456');
		    if ($is_lock == '2') {
			return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该用户已被锁定！'));
		    }
		    if (!preg_match("/^1[34578]{1}\d{9}$/", $phone)) {
			return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，账号异常无法成为代理！'), 'json');
		    }
		}
		$result = $model->where($map)->save(array('is_agent' => $state, 'password' => $password, 'is_lock' => '1', 'atime' => time()));
		if ($result) {
		    return $this->ajaxReturn(array('ret' => '1001', 'code' => $lab . '成功！', 'data' => $state));
		} else {
		    return $this->ajaxReturn(array('ret' => '1002', 'code' => $lab . '失败！', 'data' => $state));
		}
	    } else {
		return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，找不到该用户！'));
	    }
	} else {
	    return $this->ajaxReturn(array('ret' => '1002', 'code' => '无效的操作！'));
	}
    }

    /**
     * 锁定/解锁用户列表
     */
    public function userlocklist() {
	$columnname = '可锁定用户列表';
	$sex_enum = getenumbykey('sex');
	$sex_arr = getarrbyenum($sex_enum);
	$id_state_enum = getenumbykey('useridentity_state');
	$id_state_arr = getarrbyenum($id_state_enum);
	$flag_enum = getenumbykey('flag');
	$flag_arr = getarrbyenum($flag_enum);
	$state_enum = getenumbykey('userlogin_state');
	$state_arr = getarrbyenum($state_enum);
	$app_code = $this->app_code;
	$type = trim(I('type'));
	$sex = trim(I('ddlsex'));
	$idstate = trim(I('ddlidstate'));
	$flag = trim(I('ddlflag'));
	$begintime = trim(I('txtbegintime'));
	$endtime = trim(I('txtendtime'));
	$keyword = trim(I('txtkeyword'));
	$searchtype = trim(I('hidsearchtype'));

	switch ($searchtype) {
	    case '1':

		break;
	    case '2':
		if ($idstate == '3') {
		    
		} else {
		    $flag = '0';
		}
		break;
	    case '3':
		if (!empty($flag)) {
		    $idstate = '3';
		}
		break;
	    case '4':

		break;
	    default:
		break;
	}
	$appcode = trim(I('ddlapp'));

	if (!empty($app_code)) {
	    $map['app_flag'] = $app_code;
	} else {
	    if (!empty($appcode)) {
		$map['app_flag'] = $appcode;
	    }
	}
	$map['is_lock'] = '1';
	if ($type == '2') {
	    $columnname = '已锁定用户列表';
	    $flag_enum_bak = array();
	    foreach ($flag_enum as $key => $value) {
		if ($key > 1) {
		    break;
		}
		$flag_enum_bak[$key] = $value;
	    }
	    $flag_arr = getarrbyenum($flag_enum_bak);
	    $map['is_lock'] = '2';
	}
	if (!empty($sex)) {
	    if ($sex == '3') {
		$map['user_sex'] = array(array('neq', '1'), array('neq', '2'), 'and');
	    } else {
		$map['user_sex'] = $sex;
	    }
	}
	if (!empty($idstate)) {
	    if ($idstate == '1') {
		$map['state'] = array(array('exp', 'is null'), array('eq', ''), array('eq', '1'), 'or');
	    } else {
		$map['state'] = $idstate;
//		if ($idstate == '3') {
//		    if (!empty($flag)) {
//			if ($flag == '3') {
//			    $map['user_flag_switch'] = '2';
//			} else {
//			    $map['user_flag_switch'] = '1';
//			    $map['user_flag'] = $flag;
//			}
//		    }
//		}
	    }
	}
	if (!empty($flag)) {
	    if ($flag == '3') {
		$map['user_flag_switch'] = '2';
	    } else {
		$map['user_flag_switch'] = '1';
		$map['user_flag'] = $flag;
	    }
	}
	if ($type == '2') {
	    if (!empty($begintime) || !empty($endtime)) {
		if (!empty($begintime) && !empty($endtime)) {
		    $map['ltime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
		} else if (!empty($begintime)) {
		    $map['ltime'] = array('between', array(strtotime($begintime), time() + 60));
		} else if (!empty($endtime)) {
		    $map['ltime'] = array('between', array(0, strtotime($endtime) + 60));
		}
	    }
	} else {
	    if (!empty($begintime) || !empty($endtime)) {
		if (!empty($begintime) && !empty($endtime)) {
		    $map['ctime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
		} else if (!empty($begintime)) {
		    $map['ctime'] = array('between', array(strtotime($begintime), time() + 60));
		} else if (!empty($endtime)) {
		    $map['ctime'] = array('between', array(0, strtotime($endtime) + 60));
		}
	    }
	}
	if (!empty($keyword)) {
	    $where['chat_userinfo.user_id'] = array('eq', $keyword);
	    $where['chat_userinfo.user_name'] = array('like', '%' . $keyword . '%');
	    $where['chat_userinfo.user_phone'] = array('like', '%' . $keyword . '%');
//	    $where['chat_userinfo.user_profession'] = array('like', '%' . $keyword . '%');
	    $where['chat_userinfo.user_describe'] = array('like', '%' . $keyword . '%');
	    $where['_logic'] = 'or';
	    $map['_complex'] = $where;
	}
	$viewModel = new ViewModel\UserViewModel();
	$total = $viewModel->where($map)->count();
	$row = '10';
	$page = new Service\PageService($total, $row);
	$fpage = $page->fpage();
	$field = array(
	    'user_id', 'user_uid', 'ctime', 'user_phone', 'is_lock', 'ltime', 'balance', 'user_logo', 'user_name', 'user_sex', 'user_birthday', 'user_profession', 'user_describe', 'user_tag', 'user_gold', 'user_flag', 'user_flag_switch', 'is_recommend', 'is_active', 'is_hot', 'ftime', 'state'
	);
	$order = 'ctime desc, id desc';
	if ($type == '2') {
	    $order = 'ltime desc, ctime desc, id desc';
	}
	$data = $viewModel->where($map)->limit($page->listfirst, $page->listRows)->field($field)->order($order)->select();
	foreach ($data as $key => $value) {
	    $data[$key]['user_sex_text'] = empty($data[$key]['user_sex']) ? $sex_enum['3'] : $sex_enum[$data[$key]['user_sex']];
	    $data[$key]['user_gold_text'] = (int) $data[$key]['user_gold'];
	    $data[$key]['balance_text'] = (int) $data[$key]['balance'];
	    $data[$key]['user_flag_text'] = $data[$key]['state'] == '3' ? ($data[$key]['user_flag_switch'] == '1' ? $flag_enum[$data[$key]['user_flag']] : '禁止上架') : '';
	    $data[$key]['user_state_text'] = $state_enum[$data[$key]['is_lock']];
	    $data[$key]['user_idstate_text'] = empty($data[$key]['state']) ? $id_state_enum['1'] : $id_state_enum[$data[$key]['state']];
	    $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
	    $data[$key]['ltime'] = date('Y-m-d H:i', $data[$key]['ltime']);
	    $data[$key]['ftime'] = date('Y-m-d H:i', $data[$key]['ftime']);
	    $data[$key]['state_lab'] = $data[$key]['is_lock'] == '1' ? '锁定' : '解锁';
	    $data[$key]['state_class'] = $data[$key]['is_lock'] == '1' ? 'btn-danger' : 'btn-info';
	}
	$search = array('appsel' => $appcode, 'sexsel' => $sex, 'flagsel' => $flag, 'idstatesel' => $idstate, 'keyword' => $keyword, 'begintime' => $begintime, 'endtime' => $endtime);
	$this->assign(array(
	    'data' => $data,
	    'fpage' => $fpage,
	    'total' => $total,
	    'sexarr' => $sex_arr,
	    'flagarr' => $flag_arr,
	    'idstatearr' => $id_state_arr,
	    'search' => $search,
	    'type' => $type,
	    'columnname' => $columnname,
	    'app_code' => $app_code,
	    'apparr' => $this->getapplist()
	));
	$this->display();
    }

    /**
     * 用户锁定/解锁提交
     * @return type
     */
    public function userlocksubmit() {
        if (!IS_POST) {
            $this->error('非法访问！');
        }
        $user_uid = I('uid');
        if (!empty($user_uid)) {
            $map['user_uid'] = $user_uid;
            $viewModel = new ViewModel\UserViewModel();
            $data = $viewModel->where($map)->find();
            $model = M('chat_useraccount');
            if ($data) {
                $state = $data['is_lock'] == '1' ? '2' : '1';
                $lab = $state == '1' ? '解锁' : '锁定';
                $result = $model->where($map)->save(array('is_login' => $state, 'ltime' => time()));
                if ($result) {
//		    newredishmset(33, "userinfo_" . $user_uid, array('is_recommend' => '1', 'is_active' => '1', 'is_hot' => '1', 'user_flag_switch' => '1', 'user_flag' => '1'));
                    $model = M('chat_userinfo');
                    $model->where($map)->save(array('is_lock' => $state, 'ltime' => time(),'is_recommend' => '1', 'is_active' => '1', 'is_hot' => '1', 'user_flag_switch' => '1', 'user_flag' => '1'));
                    return $this->ajaxReturn(array('ret' => '1001', 'code' => $lab . '成功！', 'data' => $state));
                } else {
                    return $this->ajaxReturn(array('ret' => '1002', 'code' => $lab . '失败！', 'data' => $state));
                }
            } else {
                return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，找不到该用户！'));
            }
        } else {
            return $this->ajaxReturn(array('ret' => '1002', 'code' => '无效的操作！'));
        }
    }

    /**
     * 用户通讯录列表
     */
    public function userphonebook() {
	$sex_enum = getenumbykey('sex');
	$sex_arr = getarrbyenum($sex_enum);
	$app_code = $this->app_code;
	$sex = trim(I('ddlsex'));
	$begintime = trim(I('txtbegintime'));
	$endtime = trim(I('txtendtime'));
	$keyword = trim(I('txtkeyword'));
	$appcode = trim(I('ddlapp'));
	if (!empty($app_code)) {
	    $map['app_flag'] = $app_code;
	} else {
	    if (!empty($appcode)) {
		$map['app_flag'] = $appcode;
	    }
	}
	if (!empty($sex)) {
	    if ($sex == '3') {
		$map['user_sex'] = array(array('neq', '1'), array('neq', '2'), 'and');
	    } else {
		$map['user_sex'] = $sex;
	    }
	}
	if (!empty($begintime) || !empty($endtime)) {
	    if (!empty($begintime) && !empty($endtime)) {
		$map['ctime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
	    } else if (!empty($begintime)) {
		$map['ctime'] = array('between', array(strtotime($begintime), time() + 60));
	    } else if (!empty($endtime)) {
		$map['ctime'] = array('between', array(0, strtotime($endtime) + 60));
	    }
	}
	if (!empty($keyword)) {
	    $where['chat_userinfo.user_id'] = array('eq', $keyword);
	    $where['chat_userinfo.user_name'] = array('like', '%' . $keyword . '%');
	    $where['chat_userinfo.user_phone'] = array('like', '%' . $keyword . '%');
	    $where['_logic'] = 'or';
	    $map['_complex'] = $where;
	}
	$viewModel = new ViewModel\PhonebookViewModel();
	$total = $viewModel->where($map)->count();
	$row = '10';
	$page = new Service\PageService($total, $row);
	$fpage = $page->fpage();
	$field = array(
	    'user_id', 'user_uid', 'phonebook', 'ctime', 'user_phone', 'user_id', 'user_logo', 'user_name', 'user_sex'
	);
	$data = $viewModel->where($map)->limit($page->listfirst, $page->listRows)->field($field)->order('ctime desc, id desc')->select();
	foreach ($data as $key => $value) {
	    $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
	    $phonebook_info = '';
	    $phonebook = $data[$key]['phonebook'];
	    if (!empty($phonebook)) {
		$phonebook = json_decode($phonebook, true);
		$count = count($phonebook);
		foreach ($phonebook as $k => $v) {
		    if ($k > 3) {
			$phonebook_info .= '<br />…';
			break;
		    }
		    foreach ($v as $phone => $name) {
//			$name_len = mb_strlen($name, 'utf8');
//			if (!empty($name) && $name_len > 12) {
//			    $name = mb_substr($name, 0, 5, 'utf8') . '…' . mb_substr($name, $name_len - 6, $name_len, 'utf8');
//			}
			$phone = preg_replace('# #', '', $phone);
			if ($k == 0) {
			    $phonebook_info .= '<span style="color:#ccc;">' . ($count - $k) . '、</span><span style="color:#ccc;">姓名：</span>' . $name . ' <span style="color:#ccc;">|</span> <span style="color:#ccc;">电话号码：</span>' . $phone;
			} else {
			    $phonebook_info .= '<br /><span style="color:#ccc;">' . ($count - $k) . '、</span><span style="color:#ccc;">姓名：</span>' . $name . ' <span style="color:#ccc;">|</span> <span style="color:#ccc;">电话号码：</span>' . $phone;
			}
		    }
		}
	    }
	    $data[$key]['phonebook_info'] = $phonebook_info;
	    $data[$key]['user_sex_text'] = empty($data[$key]['user_sex']) ? $sex_enum['3'] : $sex_enum[$data[$key]['user_sex']];
	    $user_info = $this->app_name2 . '号：' . $data[$key]['user_id'] . '<br />用户昵称：' . $data[$key]['user_name'] . '<br />性别：' . (empty($data[$key]['user_sex']) ? "" : $data[$key]['user_sex_text']) . '<br />手机号码：' . $data[$key]['user_phone'];
	    $data[$key]['user_info'] = $user_info;
	}
	$search = array('appsel' => $appcode, 'sexsel' => $sex, 'keyword' => $keyword, 'begintime' => $begintime, 'endtime' => $endtime);
	$this->assign(array(
	    'data' => $data,
	    'fpage' => $fpage,
	    'total' => $total,
	    'sexarr' => $sex_arr,
	    'search' => $search,
	    'app_code' => $app_code,
	    'apparr' => $this->getapplist()
	));
	$this->display();
    }

    /**
     * 用户通讯录详情
     */
    public function userphonebookdetail() {
	$sex_enum = getenumbykey('sex');
	$state_enum = getenumbykey('useridentity_state');
	$user_uid = I('uid');
	$map['user_uid'] = $user_uid;
	$field = array(
	    'user_id', 'user_uid', 'phonebook', 'ctime', 'user_phone', 'user_id', 'user_logo', 'user_name', 'user_sex'
	);
	$viewModel = new ViewModel\PhonebookViewModel();
	$data = $viewModel->where($map)->field($field)->find();
	$data['user_sex_text'] = empty($data['user_sex']) ? $sex_enum['3'] : $sex_enum[$data['user_sex']];
	$data['ctime'] = date('Y-m-d H:i', $data['ctime']);
	$phonebook_info = '<ul class="list-group clear-list">';
	$phonebook = $data['phonebook'];
	if (!empty($phonebook)) {
	    $phonebook = json_decode($phonebook, true);
	    $count = count($phonebook);
	    foreach ($phonebook as $k => $v) {
//		if ($k > 3) {
//		    $phonebook_info .= '<br />…';
//		    break;
//		}
		foreach ($v as $phone => $name) {
//			$name_len = mb_strlen($name, 'utf8');
//			if (!empty($name) && $name_len > 12) {
//			    $name = mb_substr($name, 0, 5, 'utf8') . '…' . mb_substr($name, $name_len - 6, $name_len, 'utf8');
//			}
		    $phone = preg_replace('# #', '', $phone);
		    if ($k == 0) {
			$phonebook_info .= '<li class="list-group-item"><span style="color:#ccc;">' . ($count - $k) . '、</span><span style="color:#ccc;">姓名：</span>' . $name . ' <span style="color:#ccc;">|</span> <span style="color:#ccc;">电话号码：</span>' . $phone . '</li>';
		    } else {
			$phonebook_info .= '<li class="list-group-item"><span style="color:#ccc;">' . ($count - $k) . '、</span><span style="color:#ccc;">姓名：</span>' . $name . ' <span style="color:#ccc;">|</span> <span style="color:#ccc;">电话号码：</span>' . $phone . '</li>';
		    }
		}
	    }
	}
	$phonebook_info .= '</ul>';
	$data['phonebook_info'] = $phonebook_info;
	$this->assign(array(
	    'uid' => $user_uid,
	    'data' => $data,
	));
	$this->display();
    }




    /**
     * 上架/下架用户列表
     */
    public function userflaglist() {
	$columnname = '待上架用户列表';
	$sex_enum = getenumbykey('sex');
	$sex_arr = getarrbyenum($sex_enum);
	$grade_enum = getenumbykey('anchor_grade_state');
	$grade_arr = getarrbyenum($grade_enum);
	$app_code = $this->app_code;
	$type = trim(I('type'));
	$sex = trim(I('ddlsex'));
	$grade = trim(I('ddlgrade'));
	$begintime = trim(I('txtbegintime'));
	$endtime = trim(I('txtendtime'));
	$keyword = trim(I('txtkeyword'));
	$map['is_lock'] = '1';
//	$map['state'] = '3';
        $map['user_auth'] = '2';
	$map['user_flag_switch'] = '1';
	$map['user_flag'] = '1';
	$appcode = trim(I('ddlapp'));
	if (!empty($app_code)) {
	    $map['app_flag'] = $app_code;
	} else {
	    if (!empty($appcode)) {
		$map['app_flag'] = $appcode;
	    }
	}
	if ($type == '2') {
	    $columnname = '新人用户列表';
	    $map['user_flag'] = '2';
	}
	if (!empty($sex)) {
	    if ($sex == '3') {
		$map['user_sex'] = array(array('neq', '1'), array('neq', '2'), 'and');
	    } else {
		$map['user_sex'] = $sex;
	    }
	}
	if(empty($grade)){
            $grade = 100;
	}
	if ((!empty((int)$grade) || $grade == 0) && $grade != 100){
        $map['user_vip'] = $grade;
    }
	if ($type == '2') {
	    if (!empty($begintime) || !empty($endtime)) {
		if (!empty($begintime) && !empty($endtime)) {
		    $map['ftime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
		} else if (!empty($begintime)) {
		    $map['ftime'] = array('between', array(strtotime($begintime), time() + 60));
		} else if (!empty($endtime)) {
		    $map['ftime'] = array('between', array(0, strtotime($endtime) + 60));
		}
	    }
	} else {
	    if (!empty($begintime) || !empty($endtime)) {
		if (!empty($begintime) && !empty($endtime)) {
		    $map['ctime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
		} else if (!empty($begintime)) {
		    $map['ctime'] = array('between', array(strtotime($begintime), time() + 60));
		} else if (!empty($endtime)) {
		    $map['ctime'] = array('between', array(0, strtotime($endtime) + 60));
		}
	    }
	}
	if (!empty($keyword)) {
	    $where['chat_userinfo.user_id'] = array('eq', $keyword);
	    $where['chat_userinfo.user_name'] = array('like', '%' . $keyword . '%');
	    $where['chat_userinfo.user_phone'] = array('like', '%' . $keyword . '%');
	    $where['chat_userinfo.user_profession'] = array('like', '%' . $keyword . '%');
	    $where['chat_userinfo.user_describe'] = array('like', '%' . $keyword . '%');
	    $where['_logic'] = 'or';
	    $map['_complex'] = $where;
	}
	$viewModel = new ViewModel\UserViewModel();
	$total = $viewModel->where($map)->count();
	$row = '10';
	$page = new Service\PageService($total, $row);
	$fpage = $page->fpage();
	$field = array(
	    'user_id', 'user_uid', 'ctime', 'user_phone', 'is_lock', 'ltime', 'balance', 'user_logo', 'user_name', 'user_sex','user_auth', 'user_birthday', 'user_profession', 'user_describe', 'user_tag', 'user_gold', 'user_flag', 'user_flag_switch', 'is_recommend', 'is_active', 'is_hot', 'ftime', 'state', 'app_flag','user_call_time','total_online_time','user_vip'
	);
	$order = 'ctime desc, id desc';
	if ($type == '2') {
	    $order = 'ftime desc, ctime desc, id desc';
	}

	$data = $viewModel->where($map)->limit($page->listfirst, $page->listRows)->field($field)->order($order)->select();
	$vip = M('chat_vip')->field('id,vip')->order('vip desc')->select();
	foreach ($data as $key => $value) {
	    $data[$key]['user_sex_text'] = empty($data[$key]['user_sex']) ? $sex_enum['3'] : $sex_enum[$data[$key]['user_sex']];
	    $data[$key]['user_gold_text'] = (int) $data[$key]['user_gold'];
	    $data[$key]['balance_text'] = (int) $data[$key]['balance'];
	    $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
	    $data[$key]['ltime'] = date('Y-m-d H:i', $data[$key]['ltime']);
	    $data[$key]['ftime'] = date('Y-m-d H:i', $data[$key]['ftime']);
	    $data[$key]['flag_lab'] = $data[$key]['user_flag'] == '1' ? '上架' : '下架';
	    $data[$key]['flag_class'] = $data[$key]['user_flag'] == '1' ? 'btn-info' : 'btn-danger';
	    $data[$key]['flag_switch_lab'] = $data[$key]['user_flag_switch'] == '1' ? '禁止上架' : '恢复';
	    $data[$key]['flag_switch_class'] = $data[$key]['user_flag_switch'] == '1' ? 'btn-danger' : 'btn-info';

	    $call_time_txt = $data[$key]['user_call_time']>0 ? secToTime($data[$key]['user_call_time']) : '0';
        $online_time_txt = $data[$key]['total_online_time'] >0 ? secToTime($data[$key]['total_online_time']) : '0';
	    if($data[$key]['user_vip'] > 0){
            foreach ($vip as $k =>$v){
                if($data[$key]['user_vip'] == $v['id']){
                    $vip_text=$v['vip'].'星级';
                }
            }
	    }else{
            $vip_text='新人';
        }
	    $data[$key]['user_information'] = "主播等级:$vip_text  <br />在线时长:$call_time_txt  <br />通话时长:$online_time_txt";
        $vip_text='';
        $call_time_txt='';
        $online_time_txt='';
	}
	$search = array('appsel' => $appcode, 'sexsel' => $sex, 'gradesel' => $grade, 'keyword' => $keyword, 'begintime' => $begintime, 'endtime' => $endtime);
	$this->assign(array(
	    'data' => $data,
	    'fpage' => $fpage,
	    'total' => $total,
	    'sexarr' => $sex_arr,
	    'gradearr' => $grade_arr,
	    'search' => $search,
	    'type' => $type,
	    'columnname' => $columnname,
	    'app_code' => $app_code,
	    'apparr' => $this->getapplist()
	));
	$this->display();
    }

    /**
     * 用户上架/下架提交
     * @return type
     */
    public function userflagsubmit() {
	if (!IS_POST) {
	    $this->error('非法访问！');
	}
	$user_uid = I('uid');
	if (!empty($user_uid)) {
	    $map['user_uid'] = $user_uid;
	    $viewModel = new ViewModel\UserViewModel();
	    $data = $viewModel->where($map)->find();
	    $model = M('chat_userinfo');
	    if ($data) {
		$is_lock = $data['is_lock'];
		$idstate = $data['state'];
		$user_flag_switch = $data['user_flag_switch'];
		$state = $data['user_flag'] == '1' ? '2' : '1';
		$lab = $state == '1' ? '下架' : '上架';
		if ($state == '2') {
		    if ($is_lock == '2') {
			return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该用户已被锁定！'));
		    }
		    if ($idstate != '3') {
			return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该用户还不是主播！'));
		    }
		    if ($user_flag_switch == '2') {
			return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该用户已被禁止上架！'));
		    }
		}
//		$count_imgs = M('chat_useridentity')->where($map)->count();
//		if (!$count_imgs) {
//		    return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该用户还没有相册！'));
//		}
//		newredishmset(33, "userinfo_" . $user_uid, array('is_recommend' => '1', 'is_active' => '1', 'is_hot' => '1', 'user_flag' => $state, 'user_flag_switch' => '1', 'ftime' => time()));
		$result = $model->where($map)->save(array('is_recommend' => '1', 'is_active' => '1', 'is_hot' => '1', 'user_flag' => $state, 'user_flag_switch' => '1', 'ftime' => time()));
		if ($result) {
		    return $this->ajaxReturn(array('ret' => '1001', 'code' => $lab . '成功！', 'data' => $state));
		} else {
		    return $this->ajaxReturn(array('ret' => '1002', 'code' => $lab . '失败！', 'data' => $state));
		}
	    } else {
		return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，找不到该用户！'));
	    }
	} else {
	    return $this->ajaxReturn(array('ret' => '1002', 'code' => '无效的操作！'));
	}
    }

    /**
     * 禁止上架/恢复用户列表
     */
    public function userflagswitchlist() {
	$columnname = '可禁止上架用户列表';
	$sex_enum = getenumbykey('sex');
	$sex_arr = getarrbyenum($sex_enum);
	$app_code = $this->app_code;
	$type = trim(I('type'));
	$sex = trim(I('ddlsex'));
	$begintime = trim(I('txtbegintime'));
	$endtime = trim(I('txtendtime'));
	$keyword = trim(I('txtkeyword'));
	$map['is_lock'] = '1';
	$map['state'] = '3';
	$map['user_flag_switch'] = '1';
	$map['user_flag'] = '1';
	$appcode = trim(I('ddlapp'));

	if (!empty($app_code)) {
	    $map['app_flag'] = $app_code;
	} else {
	    if (!empty($appcode)) {
		$map['app_flag'] = $appcode;
	    }
	}
	if ($type == '2') {
	    $columnname = '已禁止上架用户列表';
	    $map['user_flag_switch'] = '2';
	}
	if (!empty($sex)) {
	    if ($sex == '3') {
		$map['user_sex'] = array(array('neq', '1'), array('neq', '2'), 'and');
	    } else {
		$map['user_sex'] = $sex;
	    }
	}
	if ($type == '2') {
	    if (!empty($begintime) || !empty($endtime)) {
		if (!empty($begintime) && !empty($endtime)) {
		    $map['ftime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
		} else if (!empty($begintime)) {
		    $map['ftime'] = array('between', array(strtotime($begintime), time() + 60));
		} else if (!empty($endtime)) {
		    $map['ftime'] = array('between', array(0, strtotime($endtime) + 60));
		}
	    }
	} else {
	    if (!empty($begintime) || !empty($endtime)) {
		if (!empty($begintime) && !empty($endtime)) {
		    $map['ctime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
		} else if (!empty($begintime)) {
		    $map['ctime'] = array('between', array(strtotime($begintime), time() + 60));
		} else if (!empty($endtime)) {
		    $map['ctime'] = array('between', array(0, strtotime($endtime) + 60));
		}
	    }
	}
	if (!empty($keyword)) {
	    $where['chat_userinfo.user_id'] = array('eq', $keyword);
	    $where['chat_userinfo.user_name'] = array('like', '%' . $keyword . '%');
	    $where['chat_userinfo.user_phone'] = array('like', '%' . $keyword . '%');
	    $where['chat_userinfo.user_profession'] = array('like', '%' . $keyword . '%');
	    $where['chat_userinfo.user_describe'] = array('like', '%' . $keyword . '%');
	    $where['_logic'] = 'or';
	    $map['_complex'] = $where;
	}
	$viewModel = new ViewModel\UserViewModel();
	$total = $viewModel->where($map)->count();
	$row = '10';
	$page = new Service\PageService($total, $row);
	$fpage = $page->fpage();
	$field = array(
	    'user_id', 'user_uid', 'ctime', 'user_phone', 'is_lock', 'ltime', 'balance', 'user_logo', 'user_name', 'user_sex', 'user_birthday', 'user_profession', 'user_describe', 'user_tag', 'user_gold', 'user_flag', 'user_flag_switch', 'is_recommend', 'is_active', 'is_hot', 'ftime', 'state'
	);
	$order = 'ctime desc, id desc';
	if ($type == '2') {
	    $order = 'ftime desc, ctime desc, id desc';
	}
	$data = $viewModel->where($map)->limit($page->listfirst, $page->listRows)->field($field)->order($order)->select();
	foreach ($data as $key => $value) {
	    $data[$key]['user_sex_text'] = empty($data[$key]['user_sex']) ? $sex_enum['3'] : $sex_enum[$data[$key]['user_sex']];
	    $data[$key]['user_gold_text'] = (int) $data[$key]['user_gold'];
	    $data[$key]['balance_text'] = (int) $data[$key]['balance'];
	    $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
	    $data[$key]['ltime'] = date('Y-m-d H:i', $data[$key]['ltime']);
	    $data[$key]['ftime'] = date('Y-m-d H:i', $data[$key]['ftime']);
	    $data[$key]['flag_switch_lab'] = $data[$key]['user_flag_switch'] == '1' ? '禁止上架' : '恢复';
	    $data[$key]['flag_switch_class'] = $data[$key]['user_flag_switch'] == '1' ? 'btn-danger' : 'btn-info';
	}
	$search = array('appsel' => $appcode, 'sexsel' => $sex, 'keyword' => $keyword, 'begintime' => $begintime, 'endtime' => $endtime);
	$this->assign(array(
	    'data' => $data,
	    'fpage' => $fpage,
	    'total' => $total,
	    'sexarr' => $sex_arr,
	    'search' => $search,
	    'type' => $type,
	    'columnname' => $columnname,
	    'app_code' => $app_code,
	    'apparr' => $this->getapplist()
	));
	$this->display();
    }

    /**
     * 设置上架
     */

    public function userflagrecommend()
    {
        if (!IS_POST) {
            $this->error('非法访问！');
        }
        $user_uid = I('uid');
        if (!empty($user_uid)) {
            $model = M('chat_userinfo');
            $map['user_uid'] = $user_uid;
            $res = $model->where($map)->save(['is_recommend' => 2]);
            if ($res) {
                return $this->ajaxReturn(array('ret' => '1001', 'code' => '推荐成功！'));
            } else {
                return $this->ajaxReturn(array('ret' => '1002', 'code' => '推荐失败！'));
            }

        }
    }

    /**
     * 用户禁止上架/恢复提交
     * @return type
     */
    public function userflagswitchsubmit() {
	if (!IS_POST) {
	    $this->error('非法访问！');
	}
	$user_uid = I('uid');
	if (!empty($user_uid)) {
	    $map['user_uid'] = $user_uid;
	    $viewModel = new ViewModel\UserViewModel();
	    $data = $viewModel->where($map)->find();
	    $model = M('chat_userinfo');
	    if ($data) {
		$is_lock = $data['is_lock'];
		$user_flag = $data['user_flag'];
		$state = $data['user_flag_switch'] == '1' ? '2' : '1';
		$lab = $state == '1' ? '恢复' : '禁止上架';
		if ($state == '2') {
		    if ($is_lock == '2') {
			return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该用户已被锁定！'));
		    }
		    if ($user_flag == '2') {
			return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该用户已上架！'));
		    }
		}
//		newredishmset(33, "userinfo_" . $user_uid, array('is_recommend' => '1', 'is_active' => '1', 'is_hot' => '1', 'user_flag_switch' => $state, 'user_flag' => '1', 'ftime' => time()));
		$result = $model->where($map)->save(array('is_recommend' => '1', 'is_active' => '1', 'is_hot' => '1', 'user_flag_switch' => $state, 'user_flag' => '1', 'ftime' => time()));
		if ($result) {
		    return $this->ajaxReturn(array('ret' => '1001', 'code' => $lab . '成功！', 'data' => $state));
		} else {
		    return $this->ajaxReturn(array('ret' => '1002', 'code' => $lab . '失败！', 'data' => $state));
		}
	    } else {
		return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，找不到该用户！'));
	    }
	} else {
	    return $this->ajaxReturn(array('ret' => '1002', 'code' => '无效的操作！'));
	}
    }

    /**
     * 认证审核用户列表
     */
    public function useridentitylist() {
	$columnname = '待审核用户列表';
	$sex_enum = getenumbykey('sex');
	$sex_arr = getarrbyenum($sex_enum);
	$state_enum = getenumbykey('useridentity_state');
	$state_arr = getarrbyenum($state_enum);
	$app_code = $this->app_code;
	$type = trim(I('type'));
	$sex = trim(I('ddlsex'));
	$state = trim(I('ddlstate'));
	$begintime = trim(I('txtbegintime'));
	$endtime = trim(I('txtendtime'));
	$keyword = trim(I('txtkeyword'));
	$appcode = trim(I('ddlapp'));

	if (!empty($app_code)) {
	    $map['app_flag'] = $app_code;
	} else {
	    if (!empty($appcode)) {
		$map['app_flag'] = $appcode;
	    }
	}
//	$map['is_lock'] = '1';
	$map['state'] = '2';
//	$map['state'] = array(array('eq', '1'), array('eq', '2'), 'or');
	if ($type == '2') {
	    $columnname = '已审核用户列表';
	    $map['state'] = array('gt', '2');
	    if (!empty($state)) {
		$map['state'] = $state;
	    }
	}
	if (!empty($sex)) {
	    if ($sex == '3') {
		$map['user_sex'] = array(array('neq', '1'), array('neq', '2'), 'and');
	    } else {
		$map['user_sex'] = $sex;
	    }
	}
	if (!empty($begintime) || !empty($endtime)) {
	    if (!empty($begintime) && !empty($endtime)) {
		$map['ctime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
	    } else if (!empty($begintime)) {
		$map['ctime'] = array('between', array(strtotime($begintime), time() + 60));
	    } else if (!empty($endtime)) {
		$map['ctime'] = array('between', array(0, strtotime($endtime) + 60));
	    }
	}
	if (!empty($keyword)) {
	    $where['chat_useridentity.id'] = array('eq', $keyword);
	    $where['chat_userinfo.user_id'] = array('eq', $keyword);
	    $where['chat_userinfo.user_name'] = array('like', '%' . $keyword . '%');
	    $where['chat_userinfo.user_phone'] = array('like', '%' . $keyword . '%');
//	    $where['chat_userinfo.user_profession'] = array('like', '%' . $keyword . '%');
//	    $where['chat_userinfo.user_describe'] = array('like', '%' . $keyword . '%');
	    $where['_logic'] = 'or';
	    $map['_complex'] = $where;
	}
	$viewModel = new ViewModel\UseridentityViewModel();
	$total = $viewModel->where($map)->count();
	$row = '5';
	$page = new Service\PageService($total, $row);
	$fpage = $page->fpage();
	$field = array(
	    'id', 'img', 'state', 'ptime', 'ctime', 'user_id', 'user_uid', 'user_logo', 'user_name', 'user_sex', 'user_phone','user_birthday','user_height','user_weight','user_describe', 'user_tag','user_address'
	);
	$order = 'ctime desc, id desc';
	if ($type == '2') {
	    $order = 'ptime desc, ctime desc, id desc';
	}
	$data = $viewModel->where($map)->limit($page->listfirst, $page->listRows)->field($field)->order($order)->select();
	foreach ($data as $key => $value) {
	    $data[$key]['user_sex_text'] = empty($data[$key]['user_sex']) ? $sex_enum['3'] : $sex_enum[$data[$key]['user_sex']];
	    $data[$key]['state_text'] = $state_enum[$data[$key]['state']];
	    $user_info = $this->app_name2 . '号：' . $data[$key]['user_id'] . '<br />用户昵称：' . $data[$key]['user_name'] . '<br />性别：' . $data[$key]['user_sex_text'] . '<br />手机号码：' . $data[$key]['user_phone'];
	    $data[$key]['user_info'] = $user_info;
	    $user_info2 = '年龄：' . Turn_age($data[$key]['user_birthday']) . '&nbsp身高：'.$data[$key]['user_height'].'cm &nbsp体重：'.$data[$key]['user_weight'].'公斤<br />生日：'.date('Y-m-d',$data[$key]['user_birthday']).'<br />所在城市：'.$data[$key]['user_address'].'<br />形象标签：'.$data[$key]['user_tag'].'<br />个性签名：'.$data[$key]['user_describe'];
	    $data[$key]['user_info2'] = $user_info2;
	    $data[$key]['ptime'] = date('Y-m-d H:i', $data[$key]['ptime']);
	    $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
	}
	$search = array('appsel' => $appcode, 'statesel' => $state, 'sexsel' => $sex, 'keyword' => $keyword, 'begintime' => $begintime, 'endtime' => $endtime);
	$this->assign(array(
	    'type' => $type,
	    'data' => $data,
	    'fpage' => $fpage,
	    'total' => $total,
	    'statearr' => $state_arr,
	    'sexarr' => $sex_arr,
	    'search' => $search,
	    'type' => $type,
	    'columnname' => $columnname,
	    'app_code' => $app_code,
	    'apparr' => $this->getapplist()
	));
	$this->display();
    }

    /**
     * 认证审核详情
     */
    public function useridentityadd() {
	$sex_enum = getenumbykey('sex');
	$state_enum = getenumbykey('useridentity_state');
	$type = I('type');
	$user_uid = I('uid');
	$map['user_uid'] = $user_uid;
	$field = array(
	    'id', 'user_id', 'user_uid', 'img', 'state', 'ptime', 'ctime', 'user_id', 'user_phone', 'user_logo', 'user_name', 'user_sex'
	);
	$viewModel = new ViewModel\UseridentityViewModel();
	$data = $viewModel->where($map)->field($field)->find();
	$data['user_sex_text'] = empty($data['user_sex']) ? $sex_enum['3'] : $sex_enum[$data['user_sex']];
	$data['state_text'] = $state_enum[$data['state']];
	$data['ptime'] = date('Y-m-d H:i', $data['ptime']);
	$data['ctime'] = date('Y-m-d H:i', $data['ctime']);
	$user_phone = $data['user_phone'];
	$this->assign(array(
	    'type' => $type,
	    'uid' => $user_uid,
	    'phone' => $user_phone,
	    'data' => $data,
	));
	$this->display();
    }

    /**
     * 认证修改提交
     * @return type
     */
    public function useridentityaddsubmit() {
	if (!IS_POST) {
	    $this->error('非法访问！');
	}
	$user_uid = trim(I('uid'));
	$img = trim(I('img'));
	if (!empty($user_uid)) {
	    $map['user_uid'] = $user_uid;
	    $viewModel = new ViewModel\UseridentityViewModel();
	    $data = $viewModel->where($map)->find();
	    $model = M('chat_useridentity');
	    if ($data) {
		$useraccount = M('chat_useraccount')->where(array('user_uid' => $user_uid))->find();
		$is_lock = $useraccount['is_login'];
		if ($is_lock == '2') {
		    return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该用户已被锁定！'));
		}
		$result = $model->where($map)->save(array('img' => $img));
		if ($result) {
		    return $this->ajaxReturn(array('ret' => '1001', 'code' => '修改成功！', 'data' => $state));
		} else {
		    return $this->ajaxReturn(array('ret' => '1002', 'code' => '修改失败！', 'data' => $state));
		}
	    } else {
		return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该认证信息可能已被删除！'));
	    }
	} else {
	    return $this->ajaxReturn(array('ret' => '1002', 'code' => '无效的操作！'));
	}
    }

    /**
     * 用户认证审核提交（通过，驳回）
     * @return type
     */
    public function useridentitysubmit() {
        if (!IS_POST) {
            $this->error('非法访问！');
        }
        $type = trim(I('type'));
        $user_uid = trim(I('uid'));
//        $user_uidall= trim(I('uidall'));
//        $division=trim(I('division'));
//        if(!$division){return $this->ajaxReturn(array('ret' => '1002', 'code' => '请区分方式'));}
//        if($division==1){
            if (!empty($user_uid)) {
                $map['user_uid'] = $user_uid;
                $viewModel = new ViewModel\UserViewModel();
                $data = $viewModel->where($map)->find();
                $model = M('chat_useridentity');
                if ($data) {
                $is_lock = $data['is_lock'];
                $state = $type == '1' ? '3' : '4';
                $lab = $state == '3' ? '通过' : '驳回';
                if ($state == '3') {
                    if ($is_lock == '2') {
                    return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该用户已被锁定！'));
                    }
                }
                $result = $model->where($map)->save(array('state' => $state, 'ptime' => time()));
                if ($result) {
//                    newredisadd(33, "userinfo_" . $user_uid, 'auth', ($state == '3' ? '2' : '1'));
                    M('chat_userinfo')->where($map)->save(array('user_auth' => ($state == '3' ? '2' : '1')));
                    return $this->ajaxReturn(array('ret' => '1001', 'code' => $lab . '成功！', 'data' => $state));
                } else {
                    return $this->ajaxReturn(array('ret' => '1002', 'code' => $lab . '失败！', 'data' => $state));
                }
                } else {
                return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该认证信息可能已被删除！'));
                }
            } else {
                return $this->ajaxReturn(array('ret' => '1002', 'code' => '无效的操作！'));
            }
//        }else{
//            if (!empty($user_uidall)) {
//                $map['user_uid'] = array('in',$user_uidall);
//                $viewModel = new ViewModel\UserViewModel();
//                $data = $viewModel->where($map)->select();
//                $model = M('chat_useridentity');
//                if ($data) {
//                    $state = $type == '1' ? '3' : '4';
//                    $lab = $state == '3' ? '通过' : '驳回';
//                    if ($state == '3') {
//                        $is_lockall = array_column($data, 'is_lock');
//                        for($i=1;$i<count($is_lockall);$i++)
//                        {
//                            if ($is_lockall[$i-1] == '2') {
//                                return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，选中的用户中有被锁定用户！'));
//                            }
//                        }
//                    }
//                    $result = $model->where($map)->save(array('state' => $state, 'ptime' => time()));
//                    if ($result) {
//                        for($i=1;$i<count($data);$i++){
//                            newredisadd(33, "userinfo_" . $data[$i-1]['user_uid'], 'auth', ($state == '3' ? '2' : '1'));
//                        }
//                        M('chat_userinfo')->where($map)->save(array('user_auth' => ($state == '3' ? '2' : '1')));
//                        return $this->ajaxReturn(array('ret' => '1001', 'code' => $lab . '成功！', 'data' => $state));
//                    } else {
//                        return $this->ajaxReturn(array('ret' => '1002', 'code' => $lab . '失败！', 'data' => $state));
//                    }
//                } else {
//                    return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该认证信息可能已被删除！'));
//                }
//            } else {
//                return $this->ajaxReturn(array('ret' => '1002', 'code' => '无效的操作！'));
//            }
//        }
    }
    /**
     * 认证审核相册列表
     */
    public function userimgsidentitylist() {
	$columnname = '待审核相册列表';
	$sex_enum = getenumbykey('sex');
	$sex_arr = getarrbyenum($sex_enum);
	$state_enum = getenumbykey('useridentity_state');
	$state_arr = getarrbyenum($state_enum);
	$app_code = $this->app_code;
	$type = trim(I('type'));
	$sex = trim(I('ddlsex'));
	$state = trim(I('ddlstate'));
	$begintime = trim(I('txtbegintime'));
	$endtime = trim(I('txtendtime'));
	$keyword = trim(I('txtkeyword'));
	$appcode = trim(I('ddlapp'));
    $user_uid=trim(I('uid'));
	if (!empty($app_code)) {
	    $map['app_flag'] = $app_code;
	} else {
	    if (!empty($appcode)) {
		$map['app_flag'] = $appcode;
	    }
	}
//	$map['is_lock'] = '1';
	$map['state'] = '2';
//	$map['state'] = array(array('eq', '1'), array('eq', '2'), 'or');
	if ($type == '2') {
	    $columnname = '已审核相册列表';
	    $map['state'] = array('gt', '2');
	    if (!empty($state)) {
		$map['state'] = $state;
	    }
	}
	if ($type == '3') {
        $columnname = '审核相册列表';
        $map['state'] = array('gt','1');
        if (!empty($state)) {
            $map['state'] = $state;
        }
    }
    if($user_uid){
	    $map['user_uid']=$user_uid;
    }
	if (!empty($sex)) {
	    if ($sex == '3') {
		$map['user_sex'] = array(array('neq', '1'), array('neq', '2'), 'and');
	    } else {
		$map['user_sex'] = $sex;
	    }
	}
	if (!empty($begintime) || !empty($endtime)) {
	    if (!empty($begintime) && !empty($endtime)) {
		$map['ctime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
	    } else if (!empty($begintime)) {
		$map['ctime'] = array('between', array(strtotime($begintime), time() + 60));
	    } else if (!empty($endtime)) {
		$map['ctime'] = array('between', array(0, strtotime($endtime) + 60));
	    }
	}
	if (!empty($keyword)) {
	    $where['chat_userimgsidentity.id'] = array('eq', $keyword);
	    $where['chat_userinfo.user_id'] = array('eq', $keyword);
	    $where['chat_userinfo.user_name'] = array('like', '%' . $keyword . '%');
	    $where['chat_userinfo.user_phone'] = array('like', '%' . $keyword . '%');
//	    $where['chat_userinfo.user_profession'] = array('like', '%' . $keyword . '%');
//	    $where['chat_userinfo.user_describe'] = array('like', '%' . $keyword . '%');
	    $where['_logic'] = 'or';
	    $map['_complex'] = $where;
	}
	$viewModel = new ViewModel\UserimgsidentityViewModel();
	$total = $viewModel->where($map)->count();
	$row = '5';
	if($type==3){
        $row = '8';
    }
	$page = new Service\PageService($total, $row);
	$fpage = $page->fpage();
	$field = array(
	    'id', 'img', 'state', 'ptime', 'ctime', 'user_id', 'user_uid', 'user_logo', 'user_name', 'user_sex', 'user_phone'
	);
	$order = 'ctime desc, id desc';
	if ($type == '2') {
	    $order = 'ptime desc, ctime desc, id desc';
	}
	$data = $viewModel->where($map)->limit($page->listfirst, $page->listRows)->field($field)->order($order)->select();
	foreach ($data as $key => $value) {
	    $data[$key]['user_sex_text'] = empty($data[$key]['user_sex']) ? $sex_enum['3'] : $sex_enum[$data[$key]['user_sex']];
	    $data[$key]['state_text'] = $state_enum[$data[$key]['state']];
	    $user_info = $this->app_name2 . '号：' . $data[$key]['user_id'] . '<br />主播昵称：' . $data[$key]['user_name'] . '<br />性别：' . $data[$key]['user_sex_text'] . '<br />手机号码：' . $data[$key]['user_phone'];
	    $data[$key]['user_info'] = $user_info;
	    $data[$key]['ptime'] = date('Y-m-d H:i', $data[$key]['ptime']);
	    $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
	}
	$search = array('appsel' => $appcode, 'statesel' => $state, 'sexsel' => $sex, 'keyword' => $keyword, 'begintime' => $begintime, 'endtime' => $endtime);
	$this->assign(array(
	    'data' => $data,
	    'fpage' => $fpage,
	    'total' => $total,
	    'statearr' => $state_arr,
	    'sexarr' => $sex_arr,
	    'search' => $search,
	    'type' => $type,
	    'columnname' => $columnname,
	    'app_code' => $app_code,
	    'apparr' => $this->getapplist()
	));
	$this->display();
    }

    /**
     * 相册审核详情
     */
    public function userimgsidentityadd() {
	$sex_enum = getenumbykey('sex');
	$state_enum = getenumbykey('useridentity_state');
	$type = I('type');
	$id = I('id');
	$map['id'] = $id;
	$field = array(
	    'id', 'user_id', 'user_uid', 'img', 'state', 'ptime', 'ctime', 'user_id', 'user_phone', 'user_logo', 'user_name', 'user_sex'
	);
	$viewModel = new ViewModel\UserimgsidentityViewModel();
	$data = $viewModel->where($map)->field($field)->find();
	$user_uid = $data['user_uid'];
	$data['user_sex_text'] = empty($data['user_sex']) ? $sex_enum['3'] : $sex_enum[$data['user_sex']];
	$data['state_text'] = $state_enum[$data['state']];
	$data['ptime'] = date('Y-m-d H:i', $data['ptime']);
	$data['ctime'] = date('Y-m-d H:i', $data['ctime']);
	$user_phone = $data['user_phone'];
	$this->assign(array(
	    'type' => $type,
	    'id' => $id,
	    'uid' => $user_uid,
	    'phone' => $user_phone,
	    'data' => $data,
	));
	$this->display();
    }

    /**
     * 相册修改提交
     * @return type
     */
    public function userimgsidentityaddsubmit() {
	if (!IS_POST) {
	    $this->error('非法访问！');
	}
	$id = trim(I('id'));
	$img = trim(I('img'));
	if (!empty($id)) {
	    $map['id'] = $id;
	    $viewModel = new ViewModel\UserimgsidentityViewModel();
	    $data = $viewModel->where($map)->find();
	    $user_uid = $data['user_uid'];
	    $model = M('chat_userimgsidentity');
	    if ($data) {
		$useraccount = M('chat_useraccount')->where(array('user_uid' => $user_uid))->find();
		$is_lock = $useraccount['is_login'];
		if ($is_lock == '2') {
		    return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该用户已被锁定！'));
		}
		$result = $model->where($map)->save(array('img' => $img));
		if ($result) {
		    $data_imgs = $model->where($map)->select();
		    newredisdelete(34, "userimgsidentity_" . $user_uid);
		    foreach ($data_imgs as $key => $value) {
			$value['keys'] = $value['id'];
			newredislistwAdd(34, "userimgsidentity_" . $user_uid, json_encode($value));
		    }
		    return $this->ajaxReturn(array('ret' => '1001', 'code' => '修改成功！', 'data' => $state));
		} else {
		    return $this->ajaxReturn(array('ret' => '1002', 'code' => '修改失败！', 'data' => $state));
		}
	    } else {
		return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该相册信息可能已被删除！'));
	    }
	} else {
	    return $this->ajaxReturn(array('ret' => '1002', 'code' => '无效的操作！'));
	}
    }

    /**
     * 相册认证审核提交（通过，驳回）
     * @return type
     */
    public function userimgsidentitysubmit() {
        if (!IS_POST) {
            $this->error('非法访问！');
        }
        $type = trim(I('type'));
        $id = trim(I('id'));
//        $idall= trim(I('idall'));
//        $division=trim(I('division'));
//        if($division==1){
            if (!empty($id)) {
                $map['id'] = $id;
                $viewModel = new ViewModel\UserimgsidentityViewModel();
                $data = $viewModel->where($map)->find();
                $model = M('chat_userimgsidentity');
                if ($data) {
                    $user_uid = $data['user_uid'];
                    $useraccount = M('chat_useraccount')->where(array('user_uid' => $user_uid))->find();
                    $is_lock = $useraccount['is_login'];
                    $state = $type == '1' ? '3' : '4';
                    $lab = $state == '3' ? '通过' : '驳回';
                    if ($state == '3') {
                        if ($is_lock == '2') {
                        return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该用户已被锁定！'));
                        }
                    }
                    $result = $model->where($map)->save(array('state' => $state, 'ptime' => time()));
                    if ($result) {
                        $map = array();
                        $map['user_uid'] = $user_uid;
                        $data_imgs = $model->where($map)->select();
                        newredisdelete(34, "userimgsidentity_" . $user_uid);
                        foreach ($data_imgs as $key => $value) {
                        $value['keys'] = $value['id'];
                        newredislistwAdd(34, "userimgsidentity_" . $user_uid, json_encode($value));
                        }
                        return $this->ajaxReturn(array('ret' => '1001', 'code' => $lab . '成功！', 'data' => $state));
                    } else {
                        return $this->ajaxReturn(array('ret' => '1002', 'code' => $lab . '失败！', 'data' => $state));
                    }
                } else {
                return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该相册信息可能已被删除！'));
                }
            } else {
                return $this->ajaxReturn(array('ret' => '1002', 'code' => '无效的操作！'));
            }
//        }else{
//            if (!empty($idall)) {
//                $map['id'] = array('in',$idall);
//                $viewModel = new ViewModel\UserimgsidentityViewModel();
//                $data = $viewModel->where($map)->select();
//                $model = M('chat_userimgsidentity');
//                if ($data) {
//                    $user_uid_all = array_column($data,'user_uid');
//                    $user_uid_str = implode(",",$user_uid_all);
//                    $condition['user_uid']=array('in',$user_uid_str);
//                    $state = $type == '1' ? '3' : '4';
//                    $lab = $state == '3' ? '通过' : '驳回';
//                    if ($state == '3') {
//                        $is_lockall = array_column(M('chat_userinfo')->where($condition)->select(),'is_lock');
//                        for($i=1;$i<count($is_lockall);$i++)
//                        {
//                            if ($is_lockall[$i-1] == '2') {
//                                return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，选中的用户中有被锁定用户！'));
//                            }
//                        }
//                    }
//                    $result = $model->where($map)->save(array('state' => $state, 'ptime' => time()));
//                    if ($result) {
//                        for($i=1;count($user_uid_all);$i++){
//                            $map['user_uid']=$user_uid_all[$i-1];
//                            $data_imgs = $model->where($map)->select();
//                            newredisdelete(34, "userimgsidentity_" . $user_uid_all[$i-1]);
//                            foreach ($data_imgs as $key => $value) {
//                                $value['keys'] = $value['id'];
//                                newredislistwAdd(34, "userimgsidentity_" . $user_uid_all[$i-1], json_encode($value));
//                            }
//                        }
//                        return $this->ajaxReturn(array('ret' => '1001', 'code' => $lab . '成功！', 'data' => $state));
//                    } else {
//                        return $this->ajaxReturn(array('ret' => '1002', 'code' => $lab . '失败！', 'data' => $state));
//                    }
//                } else {
//                    return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该相册信息可能已被删除！'));
//                }
//            } else {
//                return $this->ajaxReturn(array('ret' => '1002', 'code' => '无效的操作！'));
//            }
//        }
    }

    /**
     * 上传图片
     */
    public function imgupload() {
	$phone = I('phone');
	$type = I('type');
	$config = [
	    'maxSize' => 1048576, // 设置附件上传大小
	    'exts' => ['jpg', 'gif', 'png', 'jpeg'], // 设置附件上传类型
	    'rootPath' => './Uploads/userlogo/', // 设置附件上传目录
	    'savePath' => '', // 设置附件上传（子）目录
	    'autoSub' => false,
	    'subName' => '',
	    'saveName' => array('uniqid', ''),
	];
	$rootPath = $config['rootPath'];
	if (!is_dir($config['rootPath'])) {
	    mkdir($config['rootPath']);
	}
	$upload = new \Think\Upload($config); // 实例化上传类
	// 上传单个文件
	$info = $upload->upload();
	if (!$info) {// 上传错误提示错误信息
	    $error = $upload->getError();
	    $data['ret'] = '1002';
	    $data['code'] = $error;
	    echo json_encode($data);
	} else {// 上传成功 获取上传文件信息
	    $access_key_id = C('ACCESS_KEY_ID');
	    $access_key_secret = C('ACCESS_KEY_SECRET');
	    $img_url = C('IMG_URL');
	    $img_url2 = C('IMG_URL2');
	    $picName = $info['logoimg']['savename'];
	    $picSize = $info['logoimg']['size'];
	    $picTmp = $info['logoimg']['tmp_name'];
	    $success = new \Vendor\OSS\OssClient($access_key_id, $access_key_secret, $img_url2);
	    $ext = explode('.', $picName)[1];
	    $picRemoteName = 'pc_' . $type . '_' . $phone . '_' . time() . rand(100000, 999999) . '.' . $ext;
	    $bucket = C('BUCKET');
	    $object = $type . '/' . $picRemoteName;
	    $filePath = $rootPath . $picName;
	    try {
		$success->uploadFile($bucket, $object, $filePath);
		unlink($rootPath . $picName);
		$img = $img_url . '/' . $object;
		$data['ret'] = '1001';
		$data['code'] = '上传成功！';
		$data['data'] = $img;
		echo json_encode($data);
	    } catch (OssException $e) {
		$response = array('ret' => '1002', 'code' => '远程上传错误！');
		echo json_encode($data);
	    }

//	    //返回本地路径
//	    $img = C('LOCALHOST_URL') . '/Uploads/userlogo/' . $info['logoimg']['savename'];
//	    $data['ret'] = '1001';
//	    $data['code'] = '上传成功！';
//	    $data['data'] = $img;
//	    echo json_encode($data);
	}
    }

    /**
     * 用户批量导入
     */
    public function userbatchimport() {
	$app_code = $this->app_code;
	$id_state_enum = getenumbykey('useridentity_state');
	$id_state_enum_bak = array();
	foreach ($id_state_enum as $key => $value) {
	    if ($key > 3) {
		break;
	    }
	    $id_state_enum_bak[$key] = $value;
	}
	$id_state_arr = getarrbyenum($id_state_enum_bak);
	$flag_enum = getenumbykey('flag');
	$flag_enum_bak = array();
	foreach ($flag_enum as $key => $value) {
	    if ($key > 2) {
		break;
	    }
	    $flag_enum_bak[$key] = $value;
	}
	$flag_arr = getarrbyenum($flag_enum_bak);
	$photo_id_state_arr = getarrbyenum($id_state_enum_bak);
	$url = C('LOCALHOST_URL');
	$this->assign(array(
	    'url' => $url,
	    'idstatearr' => $id_state_arr,
	    'flagarr' => $flag_arr,
	    'photoidstatearr' => $photo_id_state_arr,
	    'app_code' => $app_code,
	    'apparr' => $this->getapplist(),
	));
	$this->display();
    }

    /**
     * 用户批量导入提交
     */
    public function userbatchimportsubmit() {
	if (!empty($_FILES)) {
	    $app_code = $this->app_code;
	    if (empty($app_code)) {
		$app_code = I('ddlapp');
	    }
	    if (empty($app_code)) {
		$this->error('未找到项目标识');
		die;
	    }
	    $config = [
		'maxSize' => 20971520, // 设置附件上传大小
		'exts' => array('xls', 'xlsx'), // 设置附件上传类型
		'rootPath' => './Uploads/excel/', // 设置附件上传目录
		'savePath' => '', // 设置附件上传（子）目录
		'autoSub' => false,
		'subName' => '',
		'saveName' => array('uniqid', ''),
	    ];
	    $rootPath = $config['rootPath'];
	    if (!is_dir($rootPath)) {
		mkdir($rootPath);
	    }
	    $upload = new \Think\Upload($config); // 实例化上传类
	    // 上传单个文件
	    $info = $upload->upload();
	    if (!$info) {// 上传错误提示错误信息
		$this->error($upload->getError());
		die;
	    } else {// 上传成功 获取上传文件信息
		$img_url = C('IMG_URL');
		$img_url2 = C('IMG_URL2');
		Vendor('PHPExcel/Classes/PHPExcel');
		$objPHPExcel = new \PHPExcel();
		$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
			->setLastModifiedBy("Maarten Balliauw")
			->setTitle("Office 2007 XLSX Test Document")
			->setSubject("Office 2007 XLSX Test Document")
			->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
			->setKeywords("office 2007 openxml php")
			->setCategory("Test result file");
		import('Org.Util.Excel');
		$filename = $upload->rootPath . $info['file']['savename']; // 生成文件路径名
		$excel = new \Spreadsheet_Excel_Reader();
		$excel->setOutputEncoding('utf-8');
		$excel->read($filename);
		$objPHPExcel->setActiveSheetIndex(0)
			->setCellValue('A1', '头像')
			->setCellValue('B1', '昵称')
			->setCellValue('C1', '性别')
			->setCellValue('D1', '手机号码')
			->setCellValue('E1', '审核状态（未审核/待审核/已通过）')
			->setCellValue('F1', '自拍照片')
			->setCellValue('G1', '是否推荐（是/否）')
			->setCellValue('H1', '是否活跃(是/否)')
			->setCellValue('I1', '是否排行(是/否)')
			->setCellValue('J1', '上下架（待上架/已上架）')
			->setCellValue('K1', '价格/分钟')
			->setCellValue('L1', '余额')
			->setCellValue('M1', '出生日期')
			->setCellValue('N1', '职业')
			->setCellValue('O1', '个性签名')
			->setCellValue('P1', '所在地')
			->setCellValue('Q1', '相册审核状态（未审核/待审核/已通过）')
			->setCellValue('R1', '相册（最多6张）');
		$objPHPExcel->setActiveSheetIndex(0);
		$id_state_enum = getenumbykey('useridentity_state');
		$id_state_enum_bak = array();
		foreach ($id_state_enum as $key => $value) {
		    if ($key > 3) {
			break;
		    }
		    $id_state_enum_bak[$key] = $value;
		}
		$id_state_enum = $id_state_enum_bak;
		$flag_enum = getenumbykey('flag');
		$flag_enum_bak = array();
		foreach ($flag_enum as $key => $value) {
		    if ($key > 2) {
			break;
		    }
		    $flag_enum_bak[$key] = $value;
		}
		$flag_enum = $flag_enum_bak;
		$sex_enum = getenumbykey('sex');
		$yn_enum = getenumbykey('yn');
		$id_state_default = I('ddlidstate');
		$flag_default = I('ddlflag');
		$photo_id_state_default = I('ddlphotoidstate');
                $is_robot = I('is_robot');

		$system = M('chat_systemset')->order('id desc')->find();
		$ratio = $system['ratio'];
		$video_gold = $system['video_gold'];
		$init_gold = $system['init_gold'];

		$userinfomodel = M('chat_userinfo');
		$useraccountmodel = M('chat_useraccount');
		$userassetsmodel = M('chat_userwallet');
		$userauthenmodel = M('chat_useridentity');
		$userpicmodel = M('chat_userimgsidentity');
//		$hobbymodel = M('chat_hobby');
		$balancemodel = M('chat_balancelog');
		$usermembernummodel = M('chat_usermembernum');
		$userinfomodel->startTrans();
		$useraccountmodel->startTrans();
		$userassetsmodel->startTrans();
		$userauthenmodel->startTrans();
		$userpicmodel->startTrans();
//		$hobbymodel->startTrans();
		$balancemodel->startTrans();
		$usermembernummodel->startTrans();

		$index = 0;
		for ($i = 0; $i <= $excel->sheets[0]['numRows'] - 1; $i++) {
		    if ($i <= 0) {
			continue;
		    }
		    $numreg = "/\d+/";
		    $logo = trim($excel->sheets[0]['cells'][$i + 1][1]);      //头像
		    $nickname = trim($excel->sheets[0]['cells'][$i + 1][2]);      //昵称
		    $sex_text = trim($excel->sheets[0]['cells'][$i + 1][3]);      //性别
		    $sex = (string) array_search($sex_text, $sex_enum);
		    $phone = trim($excel->sheets[0]['cells'][$i + 1][4]);      //手机号码
		    $id_state_text = trim($excel->sheets[0]['cells'][$i + 1][5]);      //审核状态
		    $id_state = (string) array_search($id_state_text, $id_state_enum);
		    $userpic = trim($excel->sheets[0]['cells'][$i + 1][6]);      //自拍照片
		    $is_recom_text = trim($excel->sheets[0]['cells'][$i + 1][7]);      //是否推荐
		    $is_recom = (string) array_search($is_recom_text, $yn_enum);
		    $is_active_text = trim($excel->sheets[0]['cells'][$i + 1][8]);      //是否活跃
		    $is_active = (string) array_search($is_active_text, $yn_enum);
		    $is_hot_text = trim($excel->sheets[0]['cells'][$i + 1][9]);      //是否排行
		    $is_hot = (string) array_search($is_hot_text, $yn_enum);
		    $flag_text = trim($excel->sheets[0]['cells'][$i + 1][10]);      //上下架
		    $flag = (string) array_search($flag_text, $flag_enum);
		    $user_gold = trim($excel->sheets[0]['cells'][$i + 1][11]);      //价格/分钟
		    $balance = trim($excel->sheets[0]['cells'][$i + 1][12]);      //余额
		    $birthday = trim($excel->sheets[0]['cells'][$i + 1][13]);      //出生日期
		    $birthday_stamp = strtotime($birthday);
		    $position = trim($excel->sheets[0]['cells'][$i + 1][14]);     //职业
		    $describe = trim($excel->sheets[0]['cells'][$i + 1][15]);      //个性签名
		    $region = trim($excel->sheets[0]['cells'][$i + 1][16]);      //所在地
		    $photo_id_state_text = trim($excel->sheets[0]['cells'][$i + 1][17]);      //相册审核状态
		    $photo_id_state = (string) array_search($photo_id_state_text, $id_state_enum);
		    $photo = trim($excel->sheets[0]['cells'][$i + 1][18]);      //相册
		    $photos = json_decode($photo, true);
		    if (!empty($phone)) {
			if (!preg_match("/^1[34578]{1}\d{9}$/", $phone)) {
			    $str = "第" . ($i + 1) . "行电话号码格式不正确" . "<br>";
			    $objPHPExcel->setActiveSheetIndex(0)
				    ->setCellValue('A' . ($i + 1), $logo)
				    ->setCellValue('B' . ($i + 1), $nickname)
				    ->setCellValue('C' . ($i + 1), $sex_text)
				    ->setCellValue('D' . ($i + 1), '\'' . $phone)
				    ->setCellValue('E' . ($i + 1), $id_state_text)
				    ->setCellValue('F' . ($i + 1), $userpic)
				    ->setCellValue('G' . ($i + 1), $is_recom_text)
				    ->setCellValue('H' . ($i + 1), $is_active_text)
				    ->setCellValue('I' . ($i + 1), $is_hot_text)
				    ->setCellValue('J' . ($i + 1), $flag_text)
				    ->setCellValue('K' . ($i + 1), $user_gold)
				    ->setCellValue('L' . ($i + 1), $balance)
				    ->setCellValue('M' . ($i + 1), $birthday)
				    ->setCellValue('N' . ($i + 1), $position)
				    ->setCellValue('O' . ($i + 1), $describe)
				    ->setCellValue('P' . ($i + 1), $region)
				    ->setCellValue('Q' . ($i + 1), $photo_id_state_text)
				    ->setCellValue('R' . ($i + 1), $photo)
				    ->setCellValue('S' . ($i + 1), "本行[原数据第" . ($i + 1) . "行]电话号码格式不正确");
			    continue;
			}
		    }
		    if (!empty($authen_phone)) {
			if (!preg_match("/^1[34578]{1}\d{9}$/", $authen_phone)) {
			    $str = "第" . ($i + 1) . "行电话号码格式不正确" . "<br>";
			    $objPHPExcel->setActiveSheetIndex(0)
				    ->setCellValue('A' . ($i + 1), $logo)
				    ->setCellValue('B' . ($i + 1), $nickname)
				    ->setCellValue('C' . ($i + 1), $sex_text)
				    ->setCellValue('D' . ($i + 1), '\'' . $phone)
				    ->setCellValue('E' . ($i + 1), $id_state_text)
				    ->setCellValue('F' . ($i + 1), $userpic)
				    ->setCellValue('G' . ($i + 1), $is_recom_text)
				    ->setCellValue('H' . ($i + 1), $is_active_text)
				    ->setCellValue('I' . ($i + 1), $is_hot_text)
				    ->setCellValue('J' . ($i + 1), $flag_text)
				    ->setCellValue('K' . ($i + 1), $user_gold)
				    ->setCellValue('L' . ($i + 1), $balance)
				    ->setCellValue('M' . ($i + 1), $birthday)
				    ->setCellValue('N' . ($i + 1), $position)
				    ->setCellValue('O' . ($i + 1), $describe)
				    ->setCellValue('P' . ($i + 1), $region)
				    ->setCellValue('Q' . ($i + 1), $photo_id_state_text)
				    ->setCellValue('R' . ($i + 1), $photo)
				    ->setCellValue('S' . ($i + 1), "本行[原数据第" . ($i + 1) . "行]电话号码格式不正确");
			    continue;
			}
		    }
		    if (!empty($user_gold)) {
			if (!($user_gold >= 0)) {
			    $str = $str . "第" . ($i + 1) . "行价格格式有误，必须大于等于0" . "<br>";
			    $objPHPExcel->setActiveSheetIndex(0)
				    ->setCellValue('A' . ($i + 1), $logo)
				    ->setCellValue('B' . ($i + 1), $nickname)
				    ->setCellValue('C' . ($i + 1), $sex_text)
				    ->setCellValue('D' . ($i + 1), '\'' . $phone)
				    ->setCellValue('E' . ($i + 1), $id_state_text)
				    ->setCellValue('F' . ($i + 1), $userpic)
				    ->setCellValue('G' . ($i + 1), $is_recom_text)
				    ->setCellValue('H' . ($i + 1), $is_active_text)
				    ->setCellValue('I' . ($i + 1), $is_hot_text)
				    ->setCellValue('J' . ($i + 1), $flag_text)
				    ->setCellValue('K' . ($i + 1), $user_gold)
				    ->setCellValue('L' . ($i + 1), $balance)
				    ->setCellValue('M' . ($i + 1), $birthday)
				    ->setCellValue('N' . ($i + 1), $position)
				    ->setCellValue('O' . ($i + 1), $describe)
				    ->setCellValue('P' . ($i + 1), $region)
				    ->setCellValue('Q' . ($i + 1), $photo_id_state_text)
				    ->setCellValue('R' . ($i + 1), $photo)
				    ->setCellValue('S' . ($i + 1), "本行[原数据第" . ($i + 1) . "行]价格格式有误，必须大于等于0");
			    continue;
			}
		    }
		    if (!empty($balance)) {
			if (!($balance >= 0)) {
			    $str = $str . "第" . ($i + 1) . "行余额格式有误，必须大于等于0" . "<br>";
			    $objPHPExcel->setActiveSheetIndex(0)
				    ->setCellValue('A' . ($i + 1), $logo)
				    ->setCellValue('B' . ($i + 1), $nickname)
				    ->setCellValue('C' . ($i + 1), $sex_text)
				    ->setCellValue('D' . ($i + 1), '\'' . $phone)
				    ->setCellValue('E' . ($i + 1), $id_state_text)
				    ->setCellValue('F' . ($i + 1), $userpic)
				    ->setCellValue('G' . ($i + 1), $is_recom_text)
				    ->setCellValue('H' . ($i + 1), $is_active_text)
				    ->setCellValue('I' . ($i + 1), $is_hot_text)
				    ->setCellValue('J' . ($i + 1), $flag_text)
				    ->setCellValue('K' . ($i + 1), $user_gold)
				    ->setCellValue('L' . ($i + 1), $balance)
				    ->setCellValue('M' . ($i + 1), $birthday)
				    ->setCellValue('N' . ($i + 1), $position)
				    ->setCellValue('O' . ($i + 1), $describe)
				    ->setCellValue('P' . ($i + 1), $region)
				    ->setCellValue('Q' . ($i + 1), $photo_id_state_text)
				    ->setCellValue('R' . ($i + 1), $photo)
				    ->setCellValue('S' . ($i + 1), "本行[原数据第" . ($i + 1) . "行]余额格式有误，必须大于等于0");
			    continue;
			}
		    }
		    $user_uid = $app_code . md5($phone . $app_code);
		    $count = $userinfomodel->where(array('user_uid' => $user_uid))->count();
		    if ($count) {
			$str = $str . "第" . ($i + 1) . "行用户已存在" . "<br>";
			$objPHPExcel->setActiveSheetIndex(0)
				->setCellValue('A' . ($i + 1), $logo)
				->setCellValue('B' . ($i + 1), $nickname)
				->setCellValue('C' . ($i + 1), $sex_text)
				->setCellValue('D' . ($i + 1), '\'' . $phone)
				->setCellValue('E' . ($i + 1), $id_state_text)
				->setCellValue('F' . ($i + 1), $userpic)
				->setCellValue('G' . ($i + 1), $is_recom_text)
				->setCellValue('H' . ($i + 1), $is_active_text)
				->setCellValue('I' . ($i + 1), $is_hot_text)
				->setCellValue('J' . ($i + 1), $flag_text)
				->setCellValue('K' . ($i + 1), $user_gold)
				->setCellValue('L' . ($i + 1), $balance)
				->setCellValue('M' . ($i + 1), $birthday)
				->setCellValue('N' . ($i + 1), $position)
				->setCellValue('O' . ($i + 1), $describe)
				->setCellValue('P' . ($i + 1), $region)
				->setCellValue('Q' . ($i + 1), $photo_id_state_text)
				->setCellValue('R' . ($i + 1), $photo)
				->setCellValue('S' . ($i + 1), "本行[原数据第" . ($i + 1) . "行]用户已存在");
			continue;
		    }
		    $user_id = get_user_id($app_code);
		    if (empty($user_id)) {
			$str = $str . "第" . ($i + 1) . "行用户编号生成失败" . "<br>";
			$objPHPExcel->setActiveSheetIndex(0)
				->setCellValue('A' . ($i + 1), $logo)
				->setCellValue('B' . ($i + 1), $nickname)
				->setCellValue('C' . ($i + 1), $sex_text)
				->setCellValue('D' . ($i + 1), '\'' . $phone)
				->setCellValue('E' . ($i + 1), $id_state_text)
				->setCellValue('F' . ($i + 1), $userpic)
				->setCellValue('G' . ($i + 1), $is_recom_text)
				->setCellValue('H' . ($i + 1), $is_active_text)
				->setCellValue('I' . ($i + 1), $is_hot_text)
				->setCellValue('J' . ($i + 1), $flag_text)
				->setCellValue('K' . ($i + 1), $user_gold)
				->setCellValue('L' . ($i + 1), $balance)
				->setCellValue('M' . ($i + 1), $birthday)
				->setCellValue('N' . ($i + 1), $position)
				->setCellValue('O' . ($i + 1), $describe)
				->setCellValue('P' . ($i + 1), $region)
				->setCellValue('Q' . ($i + 1), $photo_id_state_text)
				->setCellValue('R' . ($i + 1), $photo)
				->setCellValue('S' . ($i + 1), "本行[原数据第" . ($i + 1) . "行]用户编号生成失败");
			continue;
		    }

		    $logo = empty($logo) ? '' : $img_url . '/photos/' . $logo;
		    $userpic = empty($userpic) ? '' : $img_url . '/self/' . $userpic;
		    $id_state = !empty($id_state) ? $id_state : $id_state_default;
		    $photo_id_state = !empty($photo_id_state) ? $photo_id_state : $photo_id_state_default;
		    $is_recom = !empty($is_recom) ? $is_recom : '1';
		    $is_active = !empty($is_active) ? $is_active : '1';
		    $is_hot = !empty($is_hot) ? $is_hot : '1';
		    $flag = !empty($flag) ? $flag : $flag_default;
		    $user_gold = !empty($user_gold) ? $user_gold : $video_gold;
		    if ($id_state != '3') {
			$flag = '1';
			$is_recom = '1';
			$is_active = '1';
			$is_hot = '1';
		    }
		    $data = array(
			'user_uid_parent' => '',
			'user_auth' => ($id_state == '3' ? '2' : '1'),
			'user_id' => $user_id,
			'user_uid' => $user_uid,
			'user_logo' => $logo,
			'user_name' => $nickname,
			'user_sex' => $sex,
			'user_phone' => $phone,
			'user_profession' => $position,
			'user_describe' => $describe,
			'user_address' => $region,
			'user_birthday' => $birthday_stamp,
			'fans_num' => '0',
			'state' => '4',
			'is_recommend' => $is_recom,
			'is_active' => $is_active,
			'is_hot' => $is_hot,
                        'is_robot' => $is_robot,
			'user_flag' => $flag,
			'user_flag_switch' => '1',
			'call_through_num' => '0',
			'call_num' => '0',
			'user_gold' => $user_gold,
			'ctime' => time(),
			'app_flag' => $app_code,
		    );
		    if ($flag == '2') {
			$data['ftime'] = time();
		    }
		    $res = $userinfomodel->add($data);
		    if ($res) {
			$usermembernummodel->where(array('user_id' => $user_id))->save(array('state' => '2'));
			$data['id'] = $res;
//			newredishmset(33, "userinfo_" . $user_uid, $data);
		    }

		    $data = array(
			'user_uid' => $user_uid,
			'account' => $phone,
			'password' => md5('123456'),
			'ctime' => time(),
			'app_flag' => $app_code,
		    );
		    $res = $useraccountmodel->add($data);

		    $data = array(
			'user_uid' => $user_uid,
			'balance' => $init_gold,
			'money_earn' => '0',
			'money_recharge' => '0',
			'money_share' => '0',
			'money_invite' => '0',
			'money_consume' => '0',
			'ctime' => time(),
			'app_flag' => $app_code,
		    );
		    $res = $userassetsmodel->add($data);
		    if ($res) {
			$data['id'] = $res;
//			newredishmset(33, "userwallet_" . $user_uid, $data);
			$zc_order_no = "zc" . time() . rand(100000, 999999);
			$data = array(
			    'user_uid' => $user_uid,
			    'order_no' => $zc_order_no,
			    'money' => $init_gold,
			    'balance' => $init_gold,
			    'ratio' => $ratio,
			    'state' => '1',
			    'type' => '3',
			    'toggle' => '2',
			    'content' => '注册赠送',
			    'ctime' => time(),
			    'app_flag' => $app_code,
			);
			$res = $balancemodel->add($data);
		    }

		    if ($id_state == '2' || $id_state == '3') {
			if (!empty($userpic)) {
			    
			}
			$data = array(
			    'user_uid' => $user_uid,
			    'img' => $userpic,
			    'state' => $id_state,
			    'content' => '',
			    'ctime' => time(),
			    'app_flag' => $app_code,
			);
			if ($id_state == '3') {
			    $data['ptime'] = time();
			}
			$res = $userauthenmodel->add($data);
		    }

		    if ($photo_id_state == '2' || $photo_id_state == '3') {
			//newredisdelete(34, "userimgsidentity_" . $user_uid);
			foreach ($photos as $key => $value) {
			    $photo = $value['photo'];
			    if (empty($photo)) {
				continue;
			    }
			    $photo = $img_url . '/photos/' . $photo;
			    $data = array(
				'user_uid' => $user_uid,
				'img' => $photo,
				'state' => $photo_id_state,
				'content' => '',
				'ctime' => time(),
				'app_flag' => $app_code,
			    );
			    if ($photo_id_state == '3') {
				$data['ptime'] = time();
			    }
			    $res = $userpicmodel->add($data);
			    if ($res) {
				$data['keys'] = $res;
				//newredislistwAdd(34, "userimgsidentity_" . $user_uid, json_encode($data));
			    }
			}
		    }

//		    $hobbyNamestring = '';
//		    foreach ($hobbys as $key => $value) {
//			$data = array(
//			    'user_uid' => $user_uid,
//			    'hobbyName' => $value,
//			    'ctime' => time(),
//			    'app_flag' => $app_code,
//			);
//			$res = $hobbymodel->add($data);
//			if ($res) {
//			    $hobbyNamestring = $hobbyNamestring . $value . "##";
//			}
//		    }
//		    if (!empty($hobbyNamestring)) {
//			newredisadd(33, "userinfo_" . $user_uid, "hobbydata", $hobbyNamestring);
//		    }

		    $index++;
		}
		if ($index > 0) {
		    $userinfomodel->commit();
		    $useraccountmodel->commit();
		    $userassetsmodel->commit();
		    $userauthenmodel->commit();
		    $userpicmodel->commit();
//		    $hobbymodel->commit();
		    $balancemodel->commit();

		    if ($index == $excel->sheets[0]['numRows'] - 1) {
			$this->success('成功导入数据！', 'userlist');
			flush();
			ob_flush();
		    } else {
			$objPHPExcel->getActiveSheet()->setTitle('错误记录'); //设置sheet标签的名称
			$objPHPExcel->setActiveSheetIndex(0);
			ob_end_clean();  //清空缓存
			header("Pragma: public");
			header("Expires: 0");
			header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
			header("Content-Type:application/force-download");
			header("Content-Type:application/vnd.ms-execl");
			header("Content-Type:application/octet-stream");
			header("Content-Type:application/download");
			header('Content-Disposition:attachment;filename=error.xls'); //设置文件的名称
			header("Content-Transfer-Encoding:binary");
			$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
			$objWriter->save('php://output');
			$this->success('部分数据成功导入！', 'userlist');
			flush();
			ob_flush();
		    }
		} else {
		    $userinfomodel->rollback();
		    $useraccountmodel->rollback();
		    $userassetsmodel->rollback();
		    $userauthenmodel->rollback();
		    $userpicmodel->rollback();
//		    $hobbymodel->rollback();
		    $balancemodel->rollback();
		    $usermembernummodel->rollback();

		    $objPHPExcel->getActiveSheet()->setTitle('错误记录'); //设置sheet标签的名称
		    $objPHPExcel->setActiveSheetIndex(0);
		    ob_end_clean();  //清空缓存
		    header("Pragma: public");
		    header("Expires: 0");
		    header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
		    header("Content-Type:application/force-download");
		    header("Content-Type:application/vnd.ms-execl");
		    header("Content-Type:application/octet-stream");
		    header("Content-Type:application/download");
		    header('Content-Disposition:attachment;filename=error.xls'); //设置文件的名称
		    header("Content-Transfer-Encoding:binary");
		    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		    $objWriter->save('php://output');
		    flush();
		    ob_flush();
		}
	    }
	} else {
	    $this->display('userbatchimport');
	}
    }

    /*
     * 用户视频审核列表
     */
    public function uservideosidentitylist() {
        $columnname = '视频审核列表';
        $app_code = $this->app_code;
        $state = trim(I('ddlstate'));
        $sex = trim(I('ddlsex'));
        $begintime = trim(I('txtbegintime'));
        $endtime = trim(I('txtendtime'));
        $keyword = trim(I('txtkeyword'));
        $appcode = trim(I('ddlapp'));
        $sex_enum = getenumbykey('sex');
        $sex_arr = getarrbyenum($sex_enum);
        $state_enum = getenumbykey('useridentity_state');
        $state_arr = getarrbyenum($state_enum);
        if (!empty($app_code)) {
            $map['app_flag'] = $app_code;
        } else {
            if (!empty($appcode)) {
                $map['app_flag'] = $appcode;
            }
        }
        $map['state'] = array(array('exp', 'is not null'), array('neq', '1'), 'and');
        $map['is_lock'] = array('neq','2');
        if (!empty($state)) {
            $map['state'] = $state;
        }else{
            $map['state']= $state =2;
        }
        if (!empty($sex)) {
            if ($sex == '3') {
                $map['user_sex'] = array(array('neq', '1'), array('neq', '2'), 'and');
            } else {
                $map['user_sex'] = $sex;
            }
        }
        if (!empty($begintime) || !empty($endtime)) {
            if (!empty($begintime) && !empty($endtime)) {
                $map['ctime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
            } else if (!empty($begintime)) {
                $map['ctime'] = array('between', array(strtotime($begintime), time() + 60));
            } else if (!empty($endtime)) {
                $map['ctime'] = array('between', array(0, strtotime($endtime) + 60));
            }
        }
        if (!empty($keyword)) {
            $where['chat_uservideosidentity.id'] = array('eq', $keyword);
            $where['chat_userinfo.user_id'] = array('eq', $keyword);
            $where['chat_userinfo.user_name'] = array('like', '%' . $keyword . '%');
            $where['chat_userinfo.user_phone'] = array('like', '%' . $keyword . '%');
//	    $where['chat_userinfo.user_profession'] = array('like', '%' . $keyword . '%');
//	    $where['chat_userinfo.user_describe'] = array('like', '%' . $keyword . '%');  279180
            $where['_logic'] = 'or';
            $map['_complex'] = $where;
        }
        $field = array(
            'id', 'video', 'state', 'content', 'ptime', 'ctime', 'video_img', 'is_lock', 'user_id', 'user_uid', 'user_logo', 'user_name', 'user_sex', 'user_phone'
        );
        $viewModel = new ViewModel\UservideosidentityViewModel();
        $total = $viewModel->where($map)->count();
        $row = '5';
        $page = new Service\PageService($total, $row);
        $fpage = $page->fpage();

        $order = 'ctime desc, id desc';
        $data = $viewModel->where($map)->limit($page->listfirst, $page->listRows)->field($field)->order($order)->select();
        $video_id_arr = array_column($data, 'id');
        $video_id_str = null;
        if($video_id_arr) {
            foreach ($video_id_arr as $key => $value) {
                $video_id_str .= $value . ', ';
            }
            $video_id_str = rtrim($video_id_str, ", ");
            $like_where['video_id']=array('in',$video_id_str);
            $like_count = M('chat_userlike')->where($like_where)->field('count(id) as help, video_id')->group('video_id')->select();
        }

        foreach ($data as $key => $value) {
            $video_img = empty($value['video_img']) ? '' : '<img alt="未找到图片" src="' . $value['video_img'] . '" class="img2" />';
            $video_html = empty($value['video']) ? '' : '<video src="' . $value['video'] . '" controls="controls">Your browser does not support the video element.</video>';
            $user_logo_html = empty($value['user_logo']) ? '' : '<img alt="未找到图片" src="' . $value['user_logo'] . '" class="img" />';
            $user_sex_html = empty($data[$key]['user_sex']) ? $sex_enum['3'] : $sex_enum[$data[$key]['user_sex']];

            $data[$key]['video_img_text'] = $video_img;
            $data[$key]['video_state_text'] = $state_enum[$data[$key]['state']];
            $data[$key]['video_ptime'] = $data[$key]['state'] <= 2 ? '' : date('Y-m-d H:i', $data[$key]['ptime']);
            $data[$key]['video_ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
            $data[$key]['video_info'] = $video_html;
            $data[$key]['user_logo_text'] = $user_logo_html;
            $data[$key]['user_sex_text'] = $user_sex_html;
            $user_info = $this->app_name2 . '号：' . $data[$key]['user_id'] . '<br />用户昵称：' . $data[$key]['user_name'] . '<br />性别：' . $data[$key]['user_sex_text'] . '<br />手机号码：' . $data[$key]['user_phone'];
            $data[$key]['user_info'] = $user_info;
            $data[$key]['help'] = '0' ;
            if($like_count){
                foreach ($like_count as $k=>$v){
                    if($data[$key]['id'] == $v['video_id']){$data[$key]['help'] = $v['help'] ;};
                }
            }
        }
        $search = array('appsel' => $appcode, 'statesel' => $state, 'sexsel' => $sex, 'keyword' => $keyword, 'begintime' => $begintime, 'endtime' => $endtime);
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'total' => $total,
            'statearr' => $state_arr,
            'sexarr' => $sex_arr,
            'search' => $search,
            'columnname' => $columnname,
            'app_code' => $app_code,
            'apparr' => $this->getapplist()
        ));
        $this->display();
    }
    /**
     * 视频审核详情
     */
    public function uservideosidentityadd() {
        $sex_enum = getenumbykey('sex');
        $state_enum = getenumbykey('useridentity_state');
        $video_id = I('videoid');
        $user_uid = I('uid');
        $columnname = '视频审核详情';
        $map['user_uid'] = $user_uid;
        $map['id'] = $video_id;
        $field = array(
            'id',  'video', 'video_img', 'video_time', 'state', 'content', 'app_flag', 'ptime', 'ctime', 'video_img', 'user_id', 'user_uid', 'user_phone', 'user_logo', 'user_name', 'user_sex'
        );
        $viewModel = new ViewModel\UservideosidentityViewModel();
        $data = $viewModel->where($map)->field($field)->find();
        $data['user_sex_text'] = empty($data['user_sex']) ? $sex_enum['3'] : $sex_enum[$data['user_sex']];
        $data['video_state_text'] = $state_enum[$data['state']];
        $data['video_ptime'] = date('Y-m-d H:i', $data['ptime']);
        $data['video_ctime'] = date('Y-m-d H:i', $data['ctime']);
        $user_phone = $data['user_phone'];
        $this->assign(array(
            'uid' => $user_uid,
            'phone' => $user_phone,
            'data' => $data,
            'columnname' => $columnname,
        ));
        $this->display();
    }

    /*
     * 用户视频审核列表提交
     */
    public function uservideosidentitysubmit(){
        if (!IS_POST) {
            $this->error('非法访问！');
        }
        $otype = trim(I('otype'));
        $user_uid = trim(I('uid'));
        $video_id=trim(I('videoid'));
//        $videoid_str_all=trim(I('videoidall'));
//        $division=trim(I('division'));
//        if($division==1){
            if (!empty($user_uid)) {
                $map['user_uid'] = $user_uid;
                $viewModel = new ViewModel\UserViewModel();
                $data = $viewModel->where($map)->find();
                $model = M('chat_uservideosidentity');
                $where['id']=$video_id;
                if ($data) {
                    $is_lock = $data['is_lock'];
                    $state = $otype == '1' ? '3' : '4';
                    $lab = $state == '3' ? '通过' : '驳回';
                    if ($state == '3') {
                        if ($is_lock == '2') {
                            return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该用户已被锁定！'));
                        }
                    }
                    $result = $model->where($where)->save(array('state' => $state, 'ptime' => time()));
                    if ($result) {
    //                    newredisadd(33, "userinfo_" . $user_uid, 'auth', ($state == '3' ? '2' : '1'));
                        return $this->ajaxReturn(array('ret' => '1001', 'code' => $lab . '成功！', 'data' => $state));
                    } else {
                        return $this->ajaxReturn(array('ret' => '1002', 'code' => $lab . '失败！', 'data' => $state));
                    }
                } else {
                    return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该认证信息可能已被删除！'));
                }
            } else {
                return $this->ajaxReturn(array('ret' => '1002', 'code' => '无效的操作！'));
            }

//        }else{
//            if (!empty($videoid_str_all)) {
//                $model = M('chat_uservideosidentity');
//                $where['id']=array('in',$videoid_str_all);
//                $user_uid_arr = $model->where($where)->group('user_uid')->select();
//                $user_uid_arr = array_column($user_uid_arr,'user_uid');
//                $map['user_uid']=array('in',$user_uid_arr);
//                $data=M('chat_userinfo')->where($map)->field('is_lock')->select();
//                if ($data) {
//                    $is_lock_arr = array_column($data,'is_lock');
//                    $state = $otype == '1' ? '3' : '4';
//                    $lab = $state == '3' ? '通过' : '驳回';
//                    if ($state == '3') {
//                        for($i=1;$i<count($is_lock_arr);$i++)
//                        {
//                            if ($is_lock_arr[$i-1] == '2') {
//                                return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，选中的用户中有被锁定用户！'));
//                                break;
//                            }
//                        }
//                    }
//                    $result = $model->where($where)->save(array('state' => $state, 'ptime' => time()));
//                    if ($result) {
//                        //                    newredisadd(33, "userinfo_" . $user_uid, 'auth', ($state == '3' ? '2' : '1'));
//                        return $this->ajaxReturn(array('ret' => '1001', 'code' => $lab . '成功！', 'data' => $state));
//                    } else {
//                        return $this->ajaxReturn(array('ret' => '1002', 'code' => $lab . '失败！', 'data' => $state));
//                    }
//                } else {
//                    return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该认证信息可能已被删除！'));
//                }
//            } else {
//                return $this->ajaxReturn(array('ret' => '1002', 'code' => '无效的操作！'));
//            }
//        }

    }
    /*
     * 视频删除提交
     */
    public function uservideosidentitydelete(){
        if (!IS_POST) {
            $this->error('非法访问！');
        }
        $otype = trim(I('otype'));
        $user_uid = trim(I('uid'));
        $video_id=trim(I('videoid'));
        if(!empty($video_id)){
            $map['id'] = $video_id;
//            $viewModel = new ViewModel\UservideosidentityViewModel();
            $model = M('chat_uservideosidentity');
            $data = $model->where($map)->find();
            if ($data) {
                $result = $model->where($map)->save(array('is_lock'=>'2'));
            }
            if ($result) {
//                    newredisadd(33, "userinfo_" . $user_uid, 'auth', ($state == '3' ? '2' : '1'));
                return $this->ajaxReturn(array('ret' => '1001', 'code' => '删除成功！'));
            } else {
                return $this->ajaxReturn(array('ret' => '1002', 'code' => '删除失败！'));
            }
        }else {
            return $this->ajaxReturn(array('ret' => '1002', 'code' => '无效的操作！'));
        }
    }


    /*
    * 主播额度审核列表
    */
    public function userquotalist() {
        $columnname = '主播额度审核列表';
        $app_code = $this->app_code;
        $state = trim(I('ddlstate'));
        $sex = trim(I('ddlsex'));
        $begintime = trim(I('txtbegintime'));
        $endtime = trim(I('txtendtime'));
        $keyword = trim(I('txtkeyword'));
        $appcode = trim(I('ddlapp'));
        $sex_enum = getenumbykey('sex');
        $sex_arr = getarrbyenum($sex_enum);
        $state_enum = getenumbykey('useridentity_state');
        $state_arr = getarrbyenum($state_enum);
        if (!empty($app_code)) {
            $map['app_flag'] = $app_code;
        } else {
            if (!empty($appcode)) {
                $map['app_flag'] = $appcode;
            }
        }
        $map['state'] = array(array('exp', 'is not null'), array('neq', '1'), 'and');
        $map['is_lock'] = array('neq','2');
        if (!empty($state)) {
            $map['state'] = $state;
        }else{
            $map['state']= $state =2;
        }
        if (!empty($sex)) {
            if ($sex == '3') {
                $map['user_sex'] = array(array('neq', '1'), array('neq', '2'), 'and');
            } else {
                $map['user_sex'] = $sex;
            }
        }
        if (!empty($begintime) || !empty($endtime)) {
            if (!empty($begintime) && !empty($endtime)) {
                $map['ctime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
            } else if (!empty($begintime)) {
                $map['ctime'] = array('between', array(strtotime($begintime), time() + 60));
            } else if (!empty($endtime)) {
                $map['ctime'] = array('between', array(0, strtotime($endtime) + 60));
            }
        }
        if (!empty($keyword)) {
            $where['chat_uservideosidentity.id'] = array('eq', $keyword);
            $where['chat_userinfo.user_id'] = array('eq', $keyword);
            $where['chat_userinfo.user_name'] = array('like', '%' . $keyword . '%');
            $where['chat_userinfo.user_phone'] = array('like', '%' . $keyword . '%');
//	    $where['chat_userinfo.user_profession'] = array('like', '%' . $keyword . '%');
//	    $where['chat_userinfo.user_describe'] = array('like', '%' . $keyword . '%');  279180
            $where['_logic'] = 'or';
            $map['_complex'] = $where;
        }
        $field = array(
            'id', 'current_gold', 'target_gold', 'state', 'content', 'ptime', 'ctime', 'user_id', 'user_uid', 'user_logo', 'user_gold', 'user_name', 'user_sex', 'user_phone', 'user_vip'
        );
        $viewModel = new ViewModel\UserquotalistViewModel();
        $total = $viewModel->where($map)->count();
        $row = '10';
        $page = new Service\PageService($total, $row);
        $fpage = $page->fpage();

        $order = 'ctime desc, id desc';
        $data = $viewModel->where($map)->limit($page->listfirst, $page->listRows)->field($field)->order($order)->select();
        $vip = M('chat_vip')->field('id,vip')->order('vip desc')->select();
        foreach ($data as $key => $value) {
            $user_logo_html = empty($value['user_logo']) ? '' : '<img alt="未找到图片" src="' . $value['user_logo'] . '" class="img" />';
            $user_sex_html = empty($data[$key]['user_sex']) ? $sex_enum['3'] : $sex_enum[$data[$key]['user_sex']];
            $data[$key]['current_gold']= $data[$key]['current_gold']==(int)$data[$key]['current_gold'] ? (int)$data[$key]['current_gold'].'金币/分钟' : $data[$key]['current_gold'].'金币/分钟';
            $data[$key]['target_gold']= $data[$key]['target_gold']==(int)$data[$key]['target_gold'] ? (int)$data[$key]['target_gold'].'金币/分钟' : $data[$key]['target_gold'].'金币/分钟';
            $data[$key]['quota_state_text'] = $state_enum[$data[$key]['state']];
            $data[$key]['quota_ptime'] = $data[$key]['state'] <= 2 ? '' : date('Y-m-d H:i', $data[$key]['ptime']);
            $data[$key]['quota_ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
            $data[$key]['user_logo_text'] = $user_logo_html;
            $data[$key]['user_sex_text'] = $user_sex_html;
            $user_info = $this->app_name2 . '号：' . $data[$key]['user_id'] . '<br />用户昵称：' . $data[$key]['user_name'] . '<br />性别：' . $data[$key]['user_sex_text'] . '<br />手机号码：' . $data[$key]['user_phone'];
            $data[$key]['user_info'] = $user_info;
            $data[$key]['help'] = '0' ;
            $data[$key]['vip_text']=$data[$key]['user_vip'].'星级';

        }
        $search = array('appsel' => $appcode, 'statesel' => $state, 'sexsel' => $sex, 'keyword' => $keyword, 'begintime' => $begintime, 'endtime' => $endtime);
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'total' => $total,
            'statearr' => $state_arr,
            'sexarr' => $sex_arr,
            'search' => $search,
            'columnname' => $columnname,
            'app_code' => $app_code,
            'apparr' => $this->getapplist()
        ));
        $this->display();
    }

    /*
 * 用户额度审核列表提交
 */
    public function userquotasubmit(){
        if (!IS_POST) {
            $this->error('非法访问！');
        }
        $otype = trim(I('otype'));
        $user_uid = trim(I('uid'));
        $quota_id=trim(I('quotaid'));
        $content=trim(I('content'));
//        $division=trim(I('division'));
//        $quota_id_all=trim(I('quotaidall'));
//        if($division==1){
            if (!empty($user_uid)) {
                $map['user_uid'] = $user_uid;
                $viewModel = new ViewModel\UserViewModel();
                $data = $viewModel->where($map)->find();
                $model = M('chat_userquota');
                $where['id']=$quota_id;
                if ($data) {
                    $is_lock = $data['is_lock'];
                    $state = $otype == '1' ? '3' : '4';
                    $lab = $state == '3' ? '通过' : '驳回';
                    if ($state == '3') {
                        if ($is_lock == '2') {
                            return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该用户已被锁定！'));
                        }
                    }
                    if($state==3){
                        $target_gold=$model->where($where)->getField('target_gold');
                        $isok=$viewModel->where($map)->setField('user_gold',$target_gold);
                    }else{
                        $result = $model->where($where)->save(array('state' => $state, 'ptime' => time(),'content'=>$content));
                    }
                    if($isok){
                        $result = $model->where($where)->save(array('state' => $state, 'ptime' => time()));
                    }
                    if ($result) {
    //                    newredisadd(33, "userinfo_" . $user_uid, 'auth', ($state == '3' ? '2' : '1'));
                        return $this->ajaxReturn(array('ret' => '1001', 'code' => $lab . '成功！', 'data' => $state));
                    } else {
                        return $this->ajaxReturn(array('ret' => '1002', 'code' => $lab . '失败！', 'data' => $state));
                    }
                } else {
                    return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该认证信息可能已被删除！'));
                }
            } else {
                return $this->ajaxReturn(array('ret' => '1002', 'code' => '无效的操作！'));
            }
//        }else{
//            if (!empty($quota_id_all)) {
//                $model = M('chat_userquota');
//                $where['id']=array('in',$quota_id_all);
//                $data=$model->where($where)->group('user_uid')->select();
//                if ($data) {
//                    $user_uid_all = array_column($data,'user_uid');
//                    $user_uid_str = implode(",",$user_uid_all);
//                    $user_uid_str2 = "'".implode("','",$user_uid_all)."'";
//
//                    //跟新语句开始
//                    $sql = "UPDATE chat_userinfo SET user_gold = CASE  ";
//                    foreach ($data as $key => $value) {
//                        $sql .= sprintf("WHEN `user_uid` = '%s' THEN %g ", $value['user_uid'], $value['target_gold']);
//                    }
//                    $sql .= "END WHERE `user_uid` IN ($user_uid_str2)";
//                    //结束
//
//                    //判断是否被锁定
//                    $map['user_uid']=array('in',$user_uid_str);
//                    $data2=M('chat_userinfo')->where($map)->field('is_lock')->select();
//                    if($data2){
//                        $is_lock_arr = array_column($data2,'is_lock');
//                        $state = $otype == '1' ? '3' : '4';
//                        $lab = $state == '3' ? '通过' : '驳回';
//                        if ($state == '3') {
//                            for($i=1;$i<count($is_lock_arr);$i++)
//                            {
//                                if ($is_lock_arr[$i-1] == '2') {
//                                    return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，选中的用户中有被锁定用户！'));
//                                    break;
//                                }
//                            }
//                        }
//                    }else{
//                        return $this->ajaxReturn(array('ret' => '1002', 'code' => '选中的用户中有不存在用户'));
//                    }
//
//                    //开始数据跟新
//                    M()->startTrans();
//                    if($state==3){
////                        $target_gold=$model->where($where)->getField('target_gold');
////                        $isok=$viewModel->where($map)->setField('user_gold',$target_gold);
//                        $isok=M()->execute($sql);
//                    }else{
//                        $result = $model->where($where)->save(array('state' => $state, 'ptime' => time(),'content'=>$content));
////                        M()->rollback();
//                        M()->commit();
//                    }
//                    if($isok){
//                        $result = $model->where($where)->save(array('state' => $state, 'ptime' => time()));
//                        if($result) {
//                            M()->commit();
//                        }
//                    }
//                    if ($result) {
//                        //                    newredisadd(33, "userinfo_" . $user_uid, 'auth', ($state == '3' ? '2' : '1'));
//                        return $this->ajaxReturn(array('ret' => '1001', 'code' => $lab . '成功！', 'data' => $state));
//                    } else {
//                        M()->rollback();
//                        return $this->ajaxReturn(array('ret' => '1002', 'code' => $lab . '失败！已回滚数据', 'data' => $state));
//                    }
//                } else {
//                    return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该认证信息可能已被删除！'));
//                }
//            } else {
//                return $this->ajaxReturn(array('ret' => '1002', 'code' => '无效的操作！'));
//            }
//        }

    }
}
